Objective-C syntax error "Expected ']'" - ios

I am attempting to configure cells in a TableView. One of the things I am trying to do is change the font and color of the text in the cells. Here is the code:
- (void)configureView {
UIFont *cellFont = [UIFont fontWithName:#"Raleway-Thin" size:14];
NSDictionary *cellAttributesDictionary = [NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: cellFont];
}
At the colon after NSFontAttributeName I am getting the error Expected ']'. What is causing this error?

The proper syntax for a dictionary literal in Objective-C is #{...};
NSDictionary *cellAttributesDictionary = #{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: cellFont};

Related

Adding a custom font to a bar button item

I am attempting to add custom font to a bar button item but I am getting this crash.
-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
I'm pretty sure I have added the font correctly, it is in my info.plist and the custom font shows up in IB.
This is how I am setting the font:
NSDictionary *barButtonAppearanceDict = #{NSFontAttributeName : [UIFont fontWithName:#"OpenSans-Regular" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance]setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
Any ideas as to why I am getting this error?
This is how I am adding the font.
To add a custom font to your application, here is a useful guide:
http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
You can use this tutorial to double-check your custom font.
Whatever the method you use to add a custom font, run the below code to check the name of the font as it is loaded in your app. It will list all the fonts that are available in your app.
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
Then, you can get the name of the font in the system.
I don't know how you add your custom font, but in general, the Regular font is loaded without Regular suffix.
So, replace
NSDictionary *barButtonAppearanceDict = #{NSFontAttributeName : [UIFont fontWithName:#"OpenSans-Regular" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
with
NSDictionary *barButtonAppearanceDict = #{NSFontAttributeName : [UIFont fontWithName:#"OpenSans" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};

UINavigationBar setTitleTextAttributes with UnderLine Text

I am trying to setTextAttribute with UnderLine in all Views using this code
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor redColor],
// NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle),
(NSString *) kCTUnderlineStyleAttributeName:#(kCTUnderlineStyleDouble),
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:17]}];
but its not working , I know another approach using creating custom UILabel with NSAttributedString and setting it on TitleView but is there any other way to achieve this using appearance protocol?
NSUnderlineStyleAttributeName should do that job for you.
Try this Dude:
NSDictionary *attributes = #{
NSUnderlineStyleAttributeName: #1,
NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:17]
};
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
Updates:
I refered the Apple's documentation, It seems to be not possible through appearance protocol.
It says
"You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the text attribute keys described in NSString UIKit Additions Reference."
I tried by creating the new simple project as well. Couldn't see the line.
Apple Documentations:
UINavigationBar
NSString Keys for Text Attributes Dictionaries

NSAttributed String Won't change font [duplicate]

This question already has answers here:
UITextField attributedPlaceholder has no effect
(4 answers)
Closed 8 years ago.
I'm trying to format the placeholder text of a UITextField using an NSAttributedString. This code works successfully for the foreground color and kerning attributes. However, it won't change the font or the font size. What am I doing wrong? Thanks!
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:
#"USER NAME"
attributes:#{
NSForegroundColorAttributeName: [UIColor redColor],
NSFontAttributeName : [UIFont fontWithName:#"Georgia" size:8.0f],
NSKernAttributeName : [NSNumber numberWithFloat:3.0f]
}];
self.userNameTextField.attributedPlaceholder = [aString copy];
For me it works when I set custom font for UITextField, and then set other attributes for placeholder. My example that works:
_searchField.font = [UIFont fontWithName:fontRokkitRegular size:20];
UIColor *color = [UIColor colorWithRed:150.0f/255 green:150.0f/255 blue:150.0f/255 alpha:1];
_searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Keyword search" attributes:#{NSForegroundColorAttributeName:color}];
It looks like the placeholder font size is governed by the UITextField's font so I think you will have to subclass UITextField and add:
-(void)drawPlaceholderInRect:(CGRect)rect
{
NSString *aString = #"USER NAME";
[aString drawInRect:rect withAttributes:#{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName : [UIFont fontWithName:#"Georgia" size:8.0f], NSKernAttributeName : [NSNumber numberWithFloat:3.0f]}];
}

Attributed UITextView doesn't work with Korean symbols

Attributed UITextView doesn't work with Korean symbols. Steps to reproduce:
Add UITextView to the form.
Use the following code:
NSDictionary *attributes = #{
NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue" size:15],
NSForegroundColorAttributeName:[UIColor blackColor]};
[textView setAttributes:attributes
range:NSMakeRange(0, textView.text.length)];
Run the application, type any text on Korean (에서 보냄) and tap or press Enter.
The Korean text will disappear or will be replaced by several trash symbols. Why? How can I fix it?
P.S. The is an interesting answer on the question UITextField text disappears on every other keystroke But I'm creating UITextView object on the code.
Use This code it will help you.
//[_txtViewChallangeDescription setBackgroundColor:[UIColor redColor]];
_txtViewChallangeDescription.font = [UIFont fontWithName:kFontHelvetica size:kFontSize14];
[_txtViewChallangeDescription setTextColor:[UIColor blackColor]];
[_txtViewChallangeDescription setEditable:NO];
_txtViewChallangeDescription.delegate=self;
_txtViewChallangeDescription.scrollEnabled=NO;
_txtViewChallangeDescription.textContainer.lineFragmentPadding = 0;
_txtViewChallangeDescription.textContainerInset = UIEdgeInsetsZero;
NSDictionary *attrDict = #{
NSFontAttributeName : [UIFont fontWithName:kFontHelvetica size:kFontSize14],
NSForegroundColorAttributeName :[UIColor colorWithRed:152.0/255.0f green:132.0/255.0f blue:43.0/255.0f alpha:1.0f]
};
[_txtViewChallangeDescription setLinkTextAttributes:attrDict];
The following incorrect code works fine:
NSDictionary *attributes = #{
NSFontAttributeName:[/*UIFont fontWithName:#"HelveticaNeue" size:15],*/
NSForegroundColorAttributeName:[UIColor blackColor]};
[textView addAttributes:attributes
range:NSMakeRange(0, textView.text.length)];

Letterpress effect for UILabel in iOS 7 SDK

In the WWDC 2013 videos they show that a developer can use a letterpress effect on text.
Does anyone have any example code of how to do this/how to do this with a UILabel?
They've made it pretty simple now.
//Use any font you want or skip defining it
UIFont* font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//Use any color you want or skip defining it
UIColor* textColor = [UIColor blueColor];
NSDictionary *attrs = #{ NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSTextEffectAttributeName : NSTextEffectLetterpressStyle};
NSAttributedString* attrString = [[NSAttributedString alloc]
initWithString:note.title
attributes:attrs];
myTextLabel.attributedText = attrString;
It's part of NSAttributedString UIKitAdditions

Resources