UILabel.attributedText not show up on iPhone 4 + iOS 7.0.3 - ios

Got a iPhone 4 in the field and a strange problem, the UILabel does not show up any text. I tested it on iPhone 4S + iOS 7 simulator, it works fine.
Code:
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[colLabel.text copy]];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
colLabel.text = nil;
colLabel.attributedText = [attributeString copy];
colLabel.font = [UIFont boldSystemFontOfSize:12];

I have checked. Its showing on iPhone 4, there may be something else. Clean build and delete from device and run again

I have been played with the attributed text for a while, and find out something new:
It seems like on iOS 7.0.x, NSUnderlineStyleAttributeName does not play well with other attributes like color or font, once they are bundled together, it just will not show up the text. Only having underline style actually could draw the text like below:
NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:colLabel.text
attributes:#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle)}];
colLabel.attributedText = attrStr;
But once you add something like
colLabel.backgroundColor = [UIColor greenColor];
or
colLabel.font = [UIFont boldSystemFontOfSize:12];
It just won't show up, unless you make two changes: appended a newline character to your original string, and set the label's numberOfLines to 2.
like:
NSAttributedString* attrStr =
[[NSAttributedString alloc] initWithString:#"TEST\n" // <---
attributes:
#{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSParagraphStyleAttributeName:paragraph}];
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
myLabel.backgroundColor = [UIColor greenColor];
myLabel.attributedText = attrStr;
[myLabel sizeToFit];
myLabel.numberOfLines = 2; // <---

Related

As of iOS 10.3: UIButton ignoring negative paragraph lineSpacing in NSAttributedString

I have a UIButton which is really just 2 words, wrapped to 2 lines (I set the lines value to "2" in code later). I have been tightening up the default lineSpacing by putting in a negative value for the paragraph lineSpacing (as simplified & shown below).
As of iOS 10.3, it appears the negative value is now being ignored. While I can increase the lineSpacing with a positive value, I can not tighten up the 2 lines anymore.
Does anyone else know how to tighten this up in a UIButton? (I'm about ready to change that control up... but thought I'd post the question).
Much thanks all.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -15.0f;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSStrokeColorAttributeName,
[UIColor greenColor], NSForegroundColorAttributeName,
#(-2.0), NSStrokeWidthAttributeName,
paragraphStyle, NSParagraphStyleAttributeName,
nil];
NSMutableAttributedString *buttonTitle = [[NSMutableAttributedString alloc] initWithString:str attributes:attributes];
self.theButton.titleLabel.numberOfLines = 2;
[self.theButton setAttributedTitle:buttonTitle forState:UIControlStateNormal];
edit: Looks like this is also an issue with UILabel also.
i can change the line space using the code in iOS 10.2
NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle defaultParagraphStyle].mutableCopy;
paraStyle.alignment = NSTextAlignmentCenter;
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.paragraphSpacing = -8;
[self setAttributedTitle:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"test button\n normal", titleString] attributes:#{NSParagraphStyleAttributeName:paraStyle}] forState:UIControlStateNormal];
[self setAttributedTitle:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"test button\n selected", titleString] attributes:#{NSParagraphStyleAttributeName:paraStyle}] forState:UIControlStateSelected];
i guess maybe you can change the paragraphSpacing, it works~

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

Attributed string with NSLinkAttribute does not render on the iPhone

We have an attributed string with 3 components, two are basic strings, the third is a link. The string is being rendered by a UITextView. Strangely, the link is invisible on the iPhone, yet renders fine on the iPad. The behavior is consistent regardless of simulator or actual phone.
Attempting to change the color of the link has no effect. Interestingly, setting the underline color will show the underline... but the text is still invisible.
iOS 7, Xcode 5
Here is our code:
// put together our text for the about view, includes link to Facebook
UIFont *font = [UIFont fontWithName:#"Sansation_Bold_Italic" size:aboutText.font.pointSize];
UIFont *linkFont = [UIFont fontWithName:#"Sansation_Italic" size:aboutText.font.pointSize];
NSMutableDictionary* linkAttributes = [[NSMutableDictionary alloc] init];
[linkAttributes setObject:#"https://www.facebook.com/App" forKey:NSLinkAttributeName];
[linkAttributes setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
[linkAttributes setObject:[UIColor blueColor] forKey:NSStrokeColorAttributeName];
[linkAttributes setObject:[UIColor blueColor] forKey:NSUnderlineColorAttributeName];
[linkAttributes setObject:linkFont forKey:NSFontAttributeName];
NSAttributedString* link = [[NSAttributedString alloc] initWithString:#"Facebook" attributes:linkAttributes];
NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(#"ABOUTMSG1", nil)];
[mas appendAttributedString:link];
[mas appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(#"ABOUTMSG2", nil)]];
[aboutText setAttributedText:mas];
iPhone
iPad
Any thoughts on why the link is not rendering on the iPhone?
For some weird reason the color appears to be `ClClear.
In any case, if you set the linkTextAttributes of your UITextView to empty
(linkTextAttributes = [:] in Swift)
Then you can set custom color to your link as for any other text.

How can I set the color and alignment of attributed text in a UITextView in iOS 7?

The formatting of my textViews worked fine in iOS 6, but no longer in iOS 7. I understand with Text Kit much of the under the hood stuff has changed. It's become really quite confusing, and I'm hoping someone can help straighten it out a bit by helping me with something as simple as this.
My static UITextView originally was assigned a value for it's textColor and textAlignment properties. Then I made a NSMutableAttributedString, assigned it an attributes, then assigned it to the textView's attributedText property. The alignment and color no longer take effect in iOS 7.
How can I fix this? If these properties take no effect, than why do they exist anymore? Here's the creation of the textView:
UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)];
titleView.attributedText = title;
[self.view addSubview:titleView];
Curious, the properties are taken into account for UILabel but not for UITextView
Why don't you just add attributes for color and alignment to the attributed string similar to the way you are doing with the font?
Something like:
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
//add color
[title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)];
//add alignment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];
titleView.attributedText = title;
Edit: Assign the text first, then change the properties and this way it works.
UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
//create attributed string and change font
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:#"Welcome"];
UIFont *font = [UIFont fontWithName:#"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
//assign text first, then customize properties
titleView.attributedText = title;
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];

Underlining a certain portion of a UILabel

I need to underline a certain portion of a UILabel as the title suggests. For example: Please click ESPNSoccernet to read the latest Football News. I would like to underline the word ESPNSoccernet. This is because I want it to be clickable and it need to link to the website.
Need some guidance on doing this. If there is another way, do tell me...
for ios 6, you can use AttributedStrings
NSMutableAttributedString *yourString = [[NSMutableAttributedString alloc] initWithString:#"Please click ESPNSoccernet to read the latest Football News."];
[yourString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,25}];
label.attributedText = [yourString copy];
you can also use a 3rd party UILable library TTTAttributedLabel.
In Xcode:
Select the label and choose identity inspector.
In text choose Attributed instead of plain.
Now Select the portion of text you want to underline.
Select font and choose underline in fonts style.
There you go.
Swift 2.0:
1) Make a nsmutablestring with underline attribute and add to sampleLabel's text.
2) Add a tap gesture to sampleLabel and associate a method for further action.
override func viewDidLoad() {
super.viewDidLoad()
let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue], range: NSMakeRange(4, 4))
sampleLabel.attributedText = newsString.copy() as? NSAttributedString
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
tapGesture.numberOfTapsRequired = 1
sampleLabel.userInteractionEnabled = true
sampleLabel.addGestureRecognizer(tapGesture)
}
func tapResponse(recognizer: UITapGestureRecognizer) {
print("tap")
}
UILabel is only capable of displaying plain text strings (in iOS 6 it can now also display NSAttributedStrings, but this will not work in older iOS versions, so it is best not to rely on this), so you will not be able to do this with a label.
You can look at TTTAttributedLabel for displaying attributed text (so you can add underlines and other formatting), but you will not be able to add hyperlinks with this class.
The options you have for a clickable segment of the string are basically:
Use a plain UILabel and overlay a UIButton over the part that you want to be clickable, or
Use TTTAttributedLabel to achieve the underline effect, and a UITapGestureRecognizer to detect and handle taps (note that this will capture taps on the entire label, not just the underlined part).
For iOS 6:
UILabel *label = [[UILabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"Tap here to read the latest Football News."];
[string addAttribute:NSUnderlineStyleAttributeName value:#(1) range:NSMakeRange(4, 4)];
label.attributedText = [string copy];
For earlier iOS versions as well as iOS 6:
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"Tap here to read the latest Football News."];
[string addAttribute:NSUnderlineStyleAttributeName value:#(1) range:NSMakeRange(4, 4)];
label.text = [string copy];
Then add a gesture recogniser and implement handleTap::
UITapGestureRecognizer *recogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[label addGestureRecognizer:recogniser];
- (void)handleTap:(UITapGestureRecognizer *)recogniser {
// Handle the tap here
}
Well, i have done the same thing like this:
Make a custom button with text: ESPNSoccernet, and background clearColor
Add a label as a subview with height 1 and some background color to this button, such that "ESPNSoccernet" looks underlined.
Put the remaining text in a label adjacent to this button, so that it looks like a whole text.
Hope it helps!
Note: if you r doing only >iOS 6.0, you might wanna check the other answers.
If this app for ios 6 or later version in that case you can use NSMutableAttributedString
NSMutableAttributedString *labelText = [[NSMutableAttributedString alloc] initWithString:#"Hello this is demmy label for testing"];
[labelText addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1]
range:(NSRange){10,10}];
label.attributedText = labelText;
for less version you can use like this..
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 116, 171, 20)];
label.text = #"Hello this is demmy label for testing";
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:16];
[self.view addSubview:label];
//underline code
CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:tagcount] sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((label.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (label.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor whiteColor];
[self.view addSubview:viewUnderline];
The following code snippet produces desired result :
NSDictionary *dictAttribute = #{NSForegroundColorAttributeName:[UIColor redColor],NSUnderlineStyleAttributeName:#1};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:#"Please click ESPNSoccernet to read the latest Football News." attributes:dictAttribute];
lblText.attributedText = attrString;

Resources