আমার একাধিক ভিউ কন্ট্রোলার রয়েছে এবং আমি সবার ফন্টের রঙ লাল করতে চাই।
[[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]];
একটি অচেনা নির্বাচক ত্রুটি নিক্ষেপ করছে।
আমি এটা কিভাবে ঠিক করবো?
উত্তর:
রে ভেন্ডারলিচ থেকে:
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Arial-Bold" size:0.0],
UITextAttributeFont,
nil]];
অথবা আপনি যদি বস্তুর আক্ষরিক শৈলীর সাথে পছন্দ করেন:
[[UINavigationBar appearance] setTitleTextAttributes:@{
UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0],
}];
আইওএস 7 এবং নিম্নলিখিতগুলির জন্য সম্পাদনা করুন
ইউআইটিএফেক্সট্রিবিউটসগুলি আইওএস 7 হিসাবে হ্রাস করা হয় আপনি নিম্নলিখিতটি ব্যবহার করতে পারেন:
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithWhite:.0f alpha:1.f];
shadow.shadowOffset = CGSizeMake(0, -1);
[[UINavigationBar appearance] setTitleTextAttributes:@{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSShadowAttributeName: shadow,
NSFontAttributeName: [UIFont fontWithName:@"Arial-Bold" size:15.0f]
}];
if ([navBarInstance respondsToSelector:@selector(appearance)])
প্রথমে এটি করা উচিত কারণ এটি আইওএস সংস্করণ 5 এর নীচে ক্রাশ করবে
UITextAttributeTextColor
পক্ষে অবচিত NSForegroundColorAttributeName
আইওএস 7 এ
আইওএস 6 এর চেয়ে বড় বা সমান ডিপ্লোয়মেন্ট টার্গেটের জন্য, আপনার NSShadow
পরিবর্তে ব্যবহার করা উচিত :
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor lightGrayColor];
shadow.shadowOffset = CGSizeMake(0, -2);
NSDictionary * navBarTitleTextAttributes =
@{ NSForegroundColorAttributeName : [UIColor redColor],
NSShadowAttributeName : shadow,
NSFontAttributeName : [UIFont systemFontOfSize:14] };
[[UINavigationBar appearance] setTitleTextAttributes:navBarTitleTextAttributes];
আমি AppDelegate.m ক্লাসে কোডের কয়েকটি লাইন যুক্ত করে এটি করেছি, ফিনিশলঞ্চিং উইথঅ্যাপশন পদ্ধতি: এই কোডটি ব্যবহার করুন:
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0],UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
এটা আমার জন্য কাজ করে...
আপনার যদি সুইফটে এটি করার দরকার হয় তবে আপনি এই সেটিংসটি পেতে বা সেট করার অনুমতি দেওয়ার জন্য ইউআইএনএভিগেশনবারের জন্য একটি এক্সটেনশন তৈরি করতে পারেন।
extension UINavigationBar {
var titleColor: UIColor? {
get {
if let attributes = self.titleTextAttributes {
return attributes[NSForegroundColorAttributeName] as? UIColor
}
return nil
}
set {
if let value = newValue {
self.titleTextAttributes = [NSForegroundColorAttributeName: value]
}
}
}
var titleFont: UIFont? {
get {
if let attributes = self.titleTextAttributes {
return attributes[NSFontAttributeName] as? UIFont
}
return nil
}
set {
if let value = newValue {
self.titleTextAttributes = [NSFontAttributeName: value]
}
}
}
}
তারপরে আপনি এর মতো রঙ এবং ফন্ট সেট করতে পারেন:
navigationBar.titleColor = UIColor.redColor()
navigationBar.titleFont = UIFont.systemFontOfSize(12)
titleColor
বা titleFont
স্বতন্ত্রভাবে ব্যবহার করেন । আপনি যদি এগুলি একসাথে ব্যবহার করেন, self.titleTextAttributes
প্রতিবার ভেরিয়েবলের যখন কোনও একটি বলা হয় তখন একটি নতুন মান সেট করা হবে। নতুন অভিধান তৈরি করার সময় সেটার সম্ভবত অন্য মানটি পেতে পারে।
এটি বিশ্বব্যাপী সেটিংয়ের পরিবর্তে একক নেভিগেশনবারে একটি কাস্টম ভিউ সেট করতে ব্যবহার করা যেতে পারে
- (void)updateTitleWithString:(NSString *)title
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
[headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[headerView setAutoresizesSubviews:YES];
CGFloat headFontSize = (IS_SYSTEM_DEVICE_IPAD ? 25.0f : 19.0f);
UIFont *headFont = [UIFont boldSystemFontOfSize: headFontSize ];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByTruncatingTail];
CGSize size = [title boundingRectWithSize:CGSizeMake(190,headFontSize + 6) options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : headFont, NSParagraphStyleAttributeName : style} context:nil].size;
headerView.frame = CGRectMake(0, 0,size.width,self.navigationController.navigationBar.frame.size.height);
float labelHeight = headFontSize + 6;
float labelYLoc = ( self.navigationController.navigationBar.frame.size.height - labelHeight ) / 2;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,labelYLoc, size.width,labelHeight)];
label.backgroundColor = [UIColor clearColor];
label.adjustsFontSizeToFitWidth = YES;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = headFont;
label.text = title;
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.shadowOffset = CGSizeMake(0,-1);
label.accessibilityLabel = @"<LABEL>";
[headerView addSubview:label];
self.navigationItem.titleView = headerView;
}
কোড এই লাইন ব্যবহার করুন
UILabel *badge_Label=[[UILabel alloc]initWithFrame:CGRectMake(5,3, 15, 15)];
badge_Label.backgroundColor=[UIColor redcolor];
badge_Label.font=[UIFont systemFontOfSize:12];
[badge_Label setText:@"20"];
[self.navigationController.navigationBar addSubview:badgelabel];
আমি মনে করি এটি আপনার পক্ষে সহায়ক হবে।