WatchKit, AttributedString formatting not working - ios

I am building my first WatchKit App and am having troubles with NSAttributedString formatting as it does not seem to work as I'd expect ;)
This is my code:
UIFont *textFont = [UIFont fontWithName:#"Menlo" size:30];
UIFont *hlFont = [UIFont fontWithName:#"Menlo-Bold" size:30];
NSMutableAttributedString *information = [[NSMutableAttributedString alloc]initWithString:#"ADDED AN ENTRY OF " attributes:#{NSFontAttributeName : textFont}];
NSString *amountString = [NSString stringWithFormat:#"%.2f",(-1)*handler.amount.floatValue];
NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
NSAttributedString *amount = [[NSAttributedString alloc]initWithString:amountString attributes:#{NSFontAttributeName : hlFont, NSUnderlineStyleAttributeName : underline }];
NSAttributedString *to = [[NSMutableAttributedString alloc]initWithString:#" TO " attributes:#{NSFontAttributeName : textFont}];
NSString *categoryString = handler.category;
NSAttributedString *category = [[NSAttributedString alloc]initWithString:categoryString attributes:#{NSFontAttributeName : hlFont, NSUnderlineStyleAttributeName : underline }];
[information appendAttributedString:amount];
[information appendAttributedString:to];
[information appendAttributedString:category];
[_informationLabel setAttributedText:information];
and this the result:
Expectation
10.00 and Stuff should be underlined and in boldface.
Is there something fundamentally different to how attributed strings work on the watch than on iOS? What am I missing?
read through this: https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/TextandLabels.html

Solved it, the problem were the fonts #"Menlo".
By using
UIFont *textFont = [UIFont systemFontOfSize:20];
UIFont *hlFont = [UIFont systemFontOfSize:20];
the formatting with underlines works fine.

I don't believe you can programmatically set a label to be bold, italic, underlined....this has to be done through the actual interface in the storyboard.
According to this link
The attributes you can configure are
text
text color
font
min scale
alignment
lines
a potential workaround is to incorporate multiple labels, and set the ones you need to the right format (bold, underlined)

Related

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]}];
}

ios 6.0 setAttributedText crash

- (IBAction)pinFieldChanged:(id)sender {
UITextField *pinField = sender;
float kerninig = 76.0;
NSAttributedString *attributedString =
[[NSAttributedString alloc]
initWithString:pinField.text
attributes:
#{
NSFontAttributeName : [UIFont fontWithName:#"Helvetica Neue" size:36],
NSForegroundColorAttributeName : [UIColor colorWithRed:130.0/255.0 green:130.0/255.0 blue:130.0/255.0 alpha:1.0],
NSKernAttributeName : #(kerninig)
}];
if ([pinField respondsToSelector:#selector(setAttributedText:)]) {
[pinField setAttributedText:attributedString];
}
}
My app is crashing under ios6.0 when i'm trying to set attributed text to text field, though this selector is available iOS 6.0 and later
Can you give me any idea why this could happen?
Thanks in advance!)
iOS5 may be expects a CTFont so use
NSString* s = #"Yo ho ho and a bottle of rum!";
NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithString:s];
__block CGFloat f = 18.0;
CTFontRef basefont = CTFontCreateWithName((CFStringRef)#"Baskerville", f, NULL);
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS5bookExamples/ch23p689styledText1/p579p593styleText1/RootViewController.m
use below ios 6
NSAttributedString *stringValue= [[NSAttributedString alloc] initWithString:#"cocoalibrary.blogspot.com" attributes:#{NSForegroundColorAttributeName: [UIColor redColor]}];
NSAttributedString is not available below than IOS 6. See this link, you can found this line.
In iOS 6 and later you can use attributed strings to display formatted
text in text views, text fields, and some other controls

Appending NSAttributedString with line break returns attributed string with wrong format

I'm using NSMutableAttributedString and NSAttributedString to display a label text in two different font sizes. My approach is:
NSMutableAttributedString *muAtrStr = [[NSMutableAttributedString alloc]initWithString:#"2"];
NSAttributedString *atrStr = [[NSAttributedString alloc]initWithString:#"days" attributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Bold" size:8]}];
[muAtrStr appendAttributedString:atrStr];
Which returns me an Attributed string with "2" in font size 12 and "days" in font size 8.
However, the other scenario is to add a line break after 2. I use the following code:
NSMutableAttributedString *muAtrStr = [[NSMutableAttributedString alloc]initWithString:#"2"];
NSAttributedString *atrStr = [[NSAttributedString alloc]initWithString:#"\ndays" attributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Bold" size:8]}];
[muAtrStr appendAttributedString:atrStr];
This time attributed string applies the attribute on the full text. I get an attributed string with "2\ndays" in font size 8.
Try this below code, it works fine:-
NSMutableAttributedString *muAtrStr = [[NSMutableAttributedString alloc]initWithString:#"2"];
NSAttributedString *atrStr = [[NSAttributedString alloc]initWithString:#"\ndays" attributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Bold" size:8]}];
[muAtrStr appendAttributedString:atrStr];
self.lbl.numberOfLines = 0;
[self.lbl setAttributedText:muAtrStr];
Note:- Also put numberOfLines to 0 for allowing any number of lines
This works in Swift:
let attributedText = NSAttributedString(string: "Happy \nDays")
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.attributedText = attributedText

NSMutableAttributedString UILabel is not retaining its attributes when selected

I am having issues with retaining an attributed NSMutableString. I have a UITableView who's each UITableViewCell has an attributed text. Setting the attributed text is no problem, but upon selection, the UITableViewCell's attributes is lost. This is my code in cellForRowAtIndexPath that sets the attribute:
NSMutableAttributedString *changesStyleString_h = [[NSMutableAttributedString alloc] initWithString:#"Attributes change!" attributes:#{NSFontAttributeName: [UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor yellowColor]}];
[changesStyleString_h addAttributes:#{ NSUnderlineStyleAttributeName:#(1)} range:NSMakeRange(11, 6)];
cell.mainLabel.attributedText = changesStyleString
might i point out that mainLabel is also a UILabel, no customization there. Any help in the right direction would be greatly appreciated!
I found that I needed to set attributes on the ENTIRE string, or it would do funky things.
NSString* string = #"1 - some string"
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:string];
[string setAttributes:#{NSForegroundColorAttributeName: accent, NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:13.f]} range:NSMakeRange(0, 1)];
This would cause weird behavior when highlighting the cell.
However, when I did this:
[string setAttributes:#{NSForegroundColorAttributeName: [UIColor blackColor]} range:NSMakeRange(1, [labelTwo length] - 1)];
Everything seemed to work as expected.
Hope that helps!

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