I'm new to iOS Development..I cannot get my strings to get BOLD.
Thanks in advance!
Here's my code
- (void)viewDidLoad {
[super viewDidLoad];
NSString *sentence = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%#%#%#%#", self.name, self.adjective, self.noun, self.verb]];
NSMutableAttributedString *finalSentence = [[NSMutableAttributedString alloc] initWithString:sentence];
NSRange nameRange = NSMakeRange(0, self.name.length);
NSRange adjectiveRange = NSMakeRange(self.name.length, self.adjective.length);
NSRange nounRange = NSMakeRange((self.adjective.length + self.name.length), self.noun.length);
NSRange verbRange = NSMakeRange((self.adjective.length + self.name.length + self.noun.length), self.verb.length);
[finalSentence beginEditing];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial-BoldMT" size:17.0] range:nameRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial-BoldMT" size:17.0] range:adjectiveRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:nounRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:verbRange];
[finalSentence endEditing];
NSString *completeSentence = [finalSentence string];
self.resultTextView.text = completeSentence;
}
It's not an accident that there's a separate NSAttributedString class. The completeSentence, that you retrieve using your attributed string's string property, is a plain NSString, does not contain formatting info (because it cannot).
You have to assign the attributed string object itself to the attributedText property of your text view.
You are assigning the plain text version of your attributed text ([finalSentence string]) to the plain text property of your UITextView.
You should set the attributed text directly to the attributedText property of the UITextView -
self.resultTextView.attributedText=finalSentence
Just call
self.resultTextView.attributedText = finalSentence;
Here is code that you need
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *sentence = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%#%#%#%#", self.name, self.adjective, self.noun, self.verb]];
NSMutableAttributedString *finalSentence = [[NSMutableAttributedString alloc] initWithString:sentence];
NSRange nameRange = NSMakeRange(0, self.name.length);
NSRange adjectiveRange = NSMakeRange(self.name.length, self.adjective.length);
NSRange nounRange = NSMakeRange((self.adjective.length + self.name.length), self.noun.length);
NSRange verbRange = NSMakeRange((self.adjective.length + self.name.length + self.noun.length), self.verb.length);
[finalSentence beginEditing];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial-BoldMT" size:17.0] range:nameRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Arial-BoldMT" size:17.0] range:adjectiveRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:nounRange];
[finalSentence addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:verbRange];
[finalSentence endEditing];
self.resultTextView.attributedText = finalSentence;
}
Related
Im using the following code to bold a part of string. I want only the Shipment Ref. #: to be bolded. Following is my code
UIFont *boldFont = [UIFont boldSystemFontOfSize:12];
NSString *yourString = [NSString stringWithFormat:#"Shipment Ref. #: %# ",[[[NSUserDefaults alloc] init] valueForKey:#"shipRef"]];
NSRange boldedRange = NSMakeRange(10, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFont
range:boldedRange];
[attrString endEditing];
self.shipRefHeader.text = [attrString mutableString];
The problem is that it is not getting bolded
try this code
NSString * yourString = [NSString stringWithFormat:#"Shipment Ref. #: %# ",[[[NSUserDefaults alloc] init] valueForKey:#"shipRef"]];;
NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
NSString *boldString = #"Shipment Ref. #:";
NSRange boldRange = [yourString rangeOfString:boldString];
[attrString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:boldRange];
[self.shipRefHeader setAttributedText: attrString];
I'm building an app with minimum version of iOS 7. In the method where i draw my cells, i have this code to draw a UILabel with different UIFonts types.
//Get Strings
NSString* author = [self getUserCreatedVideo:notf];
NSString* caption = [NSString stringWithFormat:#"\"%#\"", [notf objectForKey:#"title"]];
NSString* challenge = #"challenged you to reply to";
NSString* timeToReply = #"24h to reply";
NSString* myString = [NSString stringWithFormat:#"%# %# %# %#", author, challenge, caption, timeToReply];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:myString];
UIColor* greenColor = [UIColor colorWithRed:102.0/255.0 green:204.0/255.0 blue:153.0/255.0 alpha:1];
//Author
[str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0,author.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:17.0] range:NSMakeRange(0,author.length)];
//Challenge
[str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(author.length+1,challenge.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:13.0] range:NSMakeRange(author.length+1,challenge.length)];
//Caption
[str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(author.length+1+challenge.length+1,caption.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:14.0] range:NSMakeRange(author.length+1+challenge.length+1,caption.length)];
//Time To Reply
[str addAttribute:NSForegroundColorAttributeName value:greenColor range:NSMakeRange(author.length+1+challenge.length+1+caption.length+1,timeToReply.length)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica" size:14.0] range:NSMakeRange(author.length+1+challenge.length+1+caption.length+1,4)];
cell.subtext.attributedText = str;
Now i want to calculate the UILabel text size, so that i can draw a UILabel 20px below cell.subtext. How can i achieve this?
So basically you want to calculate height of text when you know text width.
CGRect rect = [str boundingRectWithSize:CGSizeMake(cellWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
Use resulting rect.size the way you want.
Why does comparing the same NSAttributedString not work?
I have the following:
- (void)someSetupClass {
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:#"abcdefghijklmnopqrstuvwxyz"];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[aString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:(16.0)] range:NSMakeRange(0, 20)];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
_aTextView.attributedText = aString;
_aTextView.delegate = self;
[_scroll addSubview:_best];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:#"abcdefghijklmnopqrstuvwxyz"];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[aString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Light" size:(16.0)] range:NSMakeRange(0, 20)];
[aString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
if ([textView.attributedText isEqualToAttributedString:aString]) {
// Never reaches here, but why?
textView.text = #"";
// textView.textColor = [UIColor blackColor];
}
[textView becomeFirstResponder];
}
Make that textview strongly typed variable and try. Eg.
#property(nonatomic, strong)UITextView * aTextView;
Check if your textView has default font set to Helvetica/system - size 12. May be because of this last part of your attributed string is taking default font of the textView after you setText. But part of aString (uvwxyz) does not have font assigned and hence it mismatches.
Hope this helps you :)
I have read several method about bolding a part of string.
But I still can't get it work.
Here's my code
#define FONT_OPEN_BOLD(s) [UIFont fontWithName:#"OpenSans-Bold" size:s]
In viewDidLoad function
NSString *stringName = #"ShowTimes" ;
UIFont *font = FONT_OPEN_BOLD(15.0f);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringName];
[attrString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 4)];
self.title = stringName;
Any suggestion?
Thank you in advance. ^^
NSString *stringName = #"ShowTimes" ;
UIFont *font = FONT_OPEN_BOLD(15.0f);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringName];
[attrString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 4)];
//Initialize TTAttributedLabel with rect
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 150)];
//Set the attributedText property of TTAttributedLabel
label.attributedText = attrString;
//Set navigationItem.titleView to the label view we've created
self.navigationItem.titleView = label;
What you could do is use an NSAttributedString.
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(22, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
//draw attrString here...
Take a look at this handy dandy guide to drawing NSAttributedString objects with Core Text.
Is it possible to use multiple lines in the UIRefreshControl title? Whenever I add \n to the NSAttributedString only the first line will be displayed. I'm trying to set a title and on the next line some more text. So is there a workaround to use two lines of text in the UIRefreshControl?
This is the current code where only "Title Here" is displayed:
self.refreshControl = [[UIRefreshControl alloc] init];
NSString *title = #"Title Here";
NSString *subText = #"Subtext Here";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#",title,subText]];
[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:20.0f] range:NSMakeRange(0, [title length])];
[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:14.0f] range:NSMakeRange([title length],[subText length])];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [title length])];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange([title length], [subText length])];
self.refreshControl.attributedTitle = attString;
here is a tricky method: find the UILabel in the UIRefreshControl and set numberOfLines = 0.
UILabel *titleLabel = [[[[self.refreshControl subviews] firstObject] subviews] lastObject];
if (titleLabel) {
titleLabel.numberOfLines = 0;
NSString title = #"Pull to Refresh.\nUpdated Time: 09:30"; // \n for new line.
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:title];
}
This piece of code will work
NSString *title = #"Title Here";
NSString *subText = #"Subtext Here";
NSMutableAttributedString *titleAttString = [[NSMutableAttributedString alloc] initWithString:title];
NSMutableAttributedString *subTitleAttString = [[NSMutableAttributedString alloc] initWithString:subText];
[titleAttString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:20.0f] range:NSMakeRange(0, [title length])];
[subTitleAttString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:14.0f] range:NSMakeRange(0,[subTitle length])];
[titleAttString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [title length])];
[subTitleAttString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, [subTitle length])];
[titleAttString appendAttributedString:subTitleAttString];
self.refreshControl.attributedTitle = titleAttString;
The trick is to change the UILable's property of Refresh Controller to Zero.
The hack is we have to find the label through the subviews and there child as shown below.
Here is a Swift version
if let refreshLabel = refreshControl?.subviews.first?.subviews.last as? UILabel {
refreshLabel.numberOfLines = 0
}
So here is the full code sample
private var marketRefreshController = UIRefreshControl()
private var lastUpdatedDate = Date()
override func viewDidLoad() {
super.viewDidLoad()
tableView.refreshControl = marketRefreshController
if let refreshLabel = refreshControl?.subviews.first?.subviews.last as? UILabel {
refreshLabel.numberOfLines = 0
}
marketRefreshController.addTarget(self, action: #selector(refreshMarketData), for: .valueChanged)
marketRefreshController.tintColor = UIColor.blue
let textAttributes = [NSAttributedString.Key.font: UIFont.appSubTitle,
NSAttributedString.Key.foregroundColor: UIColor.blue]
let timeSinceLastUpdate = lastUpdatedDate.timeAgoSinceNow // Date Sting Converter Helper Method
let displayString = "Fetching Market Data ... \n Last Updated: \(timeSinceLastUpdate)"
marketRefreshController.attributedTitle = NSAttributedString(string: displayString,
attributes: textAttributes)
tableView.addSubview(marketRefreshController)
}
The expected output should be like -