I have a UITextView and a bar with three buttons which toggle bold, italic and underline.
I've set the TextView's text to attributed and it's "Allow Editing Attributes" option to YES.
Although the code for editing a selected text is working (here's an example):
UIFont *font = [UIFont fontWithName:#"HelveticaNeue-Light" size:20];
NSDictionary *dict = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
//edit selection
NSRange selectedRange = _noteTextView.selectedRange;
[[_noteTextView textStorage] beginEditing];
[[_noteTextView textStorage] addAttribute:NSFontAttributeName value:font range:selectedRange];
[[_noteTextView textStorage] addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:selectedRange];
[[_noteTextView textStorage] endEditing];
[[_noteTextView textStorage] setAttributes:dict range:selectedRange];
I am having problems with setting the attributes at my textview's indicator.
[[_noteTextView textStorage] addAttribute:] is not working or I might have a problem with the range.
Any advice?
Thanks in advance!
Try using this to set the range [textView rangeForUserParagraphAttributeChange];
It will set the attributes for the whole paragraph not just the selected range.
EDIT:
If there is text beyond the caret position you may have to extend the range by one character in order to ensure that subsequent characters that are typed in will fall within the range the attributes were applied to.
Applying attributes to a range does just that, it applies them to the range not to any text typed in beyond the range. If the length of the range is ZERO (i.e. nothing selected) then my guess is your attributes get applied to nothing.
Related
I have a UILabel in which I need to display two different colored strings:
Below is my code:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: lbl_question.attributedText];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:52.0f/255.0f green:104.0f/255.0f blue:165.0f/255.0f alpha:1.0f] range: NSMakeRange(0,[result integerValue]+1)];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor colorWithRed:75.0f/255.0f green:75.0f/255.0f blue:75.0f/255.0f alpha:2.0f] range: NSMakeRange([result integerValue]+1,[strq length])];
[lbl_question setAttributedText: text];
In iOS 6 it works fine but in ios 5 and earlier versions the two strings just got ovelapped everytime.
also i want to get width according to text and font. increase height according to their text.
I am sure there has to be solution...Please help me out with this....
You can do this using NSMutableAttributedString. This will work in ios 6,ios 7 and ios 8 also.
Note that NSMakeRange(startin gpoint counting from zero,number of character);
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:#"RedGreenBlue"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,3)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(8,4)];
lable.attributedText=string;
Thanks
From the NSAttributedString UIKit Additions Reference:
NSForegroundColorAttributeName The value of this attribute is a
UIColor object. Use this attribute to specify the color of the text
during rendering. If you do not specify this attribute, the text is
rendered in black. Available in iOS 6.0 and later.
You need two labels, or use something like TTTAttributedLabel or DTCoreText.
Try TTTAttributedLabel. It's a subclass of UILabel that supports NSAttributedStrings, which would make it easy to have multiple colors, fonts, and styles in the same string.
I am currently using
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
to get the size of an NSString. However, when that string includes emojis, it seems to calculate the size for the literal unicode character rather than taking into account the size of the emoji itself, rendering the returned size incorrect.
How do I correctly get the size of the string with emoji characters, as it will appear in a uilabel?
The NSString is not presenting the emoji, it's representing a string, so the sizeWithFont will only account for the string.
I would use:
CGRect labelFrame = label.frame;
labelFrame.size = [label sizeThatFits:CGSizeMake(100, 9999)];
[label setFrame:labelFrame];
or
//Alternatively
[label sizeToFit];
Bare in mind that sizeToFit calls the sizeThatFits: method, so in terms of just setting the label to the right height, sizeThatFits: is quicker, and much easier on the eye.
I struggled with this same thing for a while, attempting multiple solutions including the accepted answer, which did not work for me. I solved this by creating an NSAttributed String with the text, then using the NSAttributedString method boundingRectWithSize:options:context: to get the size of the string.
NSString *text = //some text
CGFloat maxSize = //text size constraints
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text
attributes:#{NSFontAttributeName : font
}];
CGRect boundingRect = [attributedString boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize fitSize = boundingRect.size;
I am trying to create a UILabel or UITextView with bold and normal text inside.
I have gone through the attributedstring but when I am setting this in label of my custom cell it doesn't display any text.
I have also used the UITextView setContentToHTMLString: method, but it is undocumented and app get rejected.
Can anyone give some sort of solution to this?
Use "NSAttributedString" to set multiple font text in a single label & use CATextLayer to render it:
just #import "NSAttributedString+Attributes.h"
and then implement it like this:
NSString *string1 = #"Hi";
NSString *string2 = #"How are you ?";
NSMutableAttributedString *attr1 = [NSMutableAttributedString attributedStringWithString:string1];
[attr1 setFont:[UIFont systemFontOfSize:20]];
NSMutableAttributedString *attr2 = [NSMutableAttributedString attributedStringWithString:string2]
[attr2 setFont:[UIFont boldSystemFontOfSize:20]];
[attr1 appendAttributedString:attr2]
CATextLayer *textLayer = [CATextLayer layer];
layer.string = attr1;
layer.contentsScale = [[UIScreen mainScreen] scale];
(Your_text_label).layer = textLayer;
OR (if you want to render on a view directly)
[(Your_View_Name).layer addSublayer:textLayer];
Up until iOS 6.0, you couldn't do this with a normal UILabel or UITextView, but you can use NSAttributedString objects with a few possible open source solutions.
Like TTAttributedLabel or OHAttributedLabel.
One solution built into the iOS SDK, you could also use a CATextLayer which has a string property that can be set to a NSAttributedString.
And, like the commenters below say, yes you can do this with the "attributedText" property. Horray! (for Apple listening to developer's very often repeated feature requests)
I know this is an old thread, but this is something I just discovered myself. At least in Xcode version 4.6.3 this is possible by using an attributed textView. What's even better is that it's possible to all be done in Interface Builder!
Here are the steps:
Place your textView at the desired location
Select the textView and open up the Attributes tab under the Utilities panel
Change the textView text to "attributed"
Enter your desired text
Now, highlight whatever text you want bolded, underlined, etc.
Click on the "T" button next to the fontName
In the popup, select your desired typeface (ex: Bold)
You should see the desired typeface displayed in the Utilities panel
Enjoy!
The following code is for iOS 6.0 and above. The result is that the text "This is bold" will be in bold and "This is not bold." will be normal text.
if ([self.registrationLabel respondsToSelector:#selector(setAttributedText:)])
{
// iOS6 and above : Use NSAttributedStrings
const CGFloat fontSize = 17;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
//UIColor *foregroundColor = [UIColor clearColor];
// Create the attributes
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
boldFont, NSFontAttributeName, nil];
NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
regularFont, NSFontAttributeName, nil];
const NSRange range = NSMakeRange(0,12); // range of " 2012/10/14 ". Ideally this should not be hardcoded
// Create the attributed string (text + attributes)
NSString *text = #"This is bold and this is not bold.;
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:text
attributes:subAttrs];
[attributedText setAttributes:attrs range:range];
// Set it in our UILabel and we are done!
[self.registrationLabel setAttributedText:attributedText];
}
If you have an issue with editing the attributed text in the inspector, copy/paste the text into a rich text editor, adjust it, switch the textView Text options to Attributed and paste. Vwala.
I have a UILabel which has text containing both chinese and english characters,
now I want to set a font for chinese and another font for english,
how to do this?
There are couple of things that might be of interesting to you:
OHAttributedLabel
and TTTAttributedLabel
OHAttributedLabel stays it is capable of dealing with mixed fonts, color, size, ...
Generally one label can have only one font. Still if you want to show different font for different languages than you can keep different language string in different labels and arrange them the way you want.
See this to decide size of your labels.
Resize UITableViewCell to UILabel's height dynamically
and this is also helpful.
How do I wrap text in a UITableViewCell without a custom cell
I do not believe this is possible. The font property set in a UILabel would apply to the entire string specified in the text property of that UILabel.
I've not tried using chinese font, but you can use the following code to set different / multiple fonts & other properties on Label using NSMutableAttributedString. Foll is my code:
UIFont *ArialFont = [UIFont fontWithName:#"arial" size:18.0];
NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];
NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict];
UIFont *VerdanaFont = [UIFont fontWithName:#"verdana" size:12.0];
NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];
[VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];
[AattrString appendAttributedString:VattrString];
lblText.attributedText = AattrString;
Note that lblText is the UILabel, outlet as file owner.
One can keep on appending as many NSMutableAttributedString he wants..
Also Note that I've added verdana & arial font in my project & added a plist for the same.
What is the best solution so far for a strike out text on the iPhone?
I heard of multiple solutions:
Something with three20
Image as a subview
UIWebView
And something with a NSAttributedString, but I don't find a working example for that.
In iOS 6 we can use "NSMutableAttributedString" for use more different styles.
NSString* cutText = #"This Line is strike out.";
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:cutText];
// making text property to strike text- NSStrikethroughStyleAttributeName
[titleString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])];
// using text on label
[myTextLabel setAttributedText:titleString];
You can try my solution of UILabel subclass, which supports:
multiline text with various label bounds (text can be in the middle of label frame, or accurate size)
underline
strikeout
underline/strikeout line offset
text alignment
different font sizes
https://github.com/GuntisTreulands/UnderLineLabel