NSMutableParagraphStyle *paragraph=[[NSMutableParagraphStyle
defaultParagraphStyle] mutableCopy];
[paragraph setLineBreakMode:NSLineBreakByCharWrapping];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:FONT_14, NSFontAttributeName, NSParagraphStyleAttributeName, paragraph,nil];
CGRect nvoiceStateSize = [tips boundingRectWithSize:line3size
options:(NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading)
attributes:attrs
context:nil];
it crashed when excute boundingRectWithSize:
Related
I want to create a single UILablel like this.
I want to set year font to HElvaticaNeu-medium and the 2015 into HelvaticaNeu. And I need these two strings to appear in 2 lines just like in this image.
How I can do this. I set two seperate NSMutableAttributedString in this way.
UIFont *helMedium = [UIFont fontWithName:#"HelveticaNeue-Medium" size:12.0];
NSDictionary *mediumDict = [NSDictionary dictionaryWithObject: helMedium forKey:NSFontAttributeName];
NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc] initWithString:#"Year :" attributes: mediumDict];
UIFont *helNormal = [UIFont fontWithName:#"HelveticaNeue" size:12.0];
NSDictionary *normalDict = [NSDictionary dictionaryWithObject: helNormal forKey:NSFontAttributeName];
NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:#"2016" attributes: normalDict];
Now how should I set this 2 strings in two lines?
Does anyone have an idea?
you need to append the second string to first string
Use this code.
UIFont *helMedium = [UIFont fontWithName:#"HelveticaNeue-Medium" size:12.0];
NSDictionary *mediumDict = [NSDictionary dictionaryWithObject: helMedium forKey:NSFontAttributeName];
NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc] initWithString:#"Year :" attributes: mediumDict];
UIFont *helNormal = [UIFont fontWithName:#"HelveticaNeue" size:12.0];
NSDictionary *normalDict = [NSDictionary dictionaryWithObject: helNormal forKey:NSFontAttributeName];
NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:#"\n2016" attributes: normalDict];
[mAttrString appendAttributedString:nAttrString];
label.attributedText = mAttrString;
NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:#"\n2016" attributes: normalDict];
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString: mAttrString];
[attString appendAttributedString: nAttrString];
// label.attributedText = attString
UIFont *helMedium = [UIFont fontWithName:#"HelveticaNeue-Medium" size:12.0];
NSDictionary *mediumDict = [NSDictionary dictionaryWithObject: helMedium forKey:NSFontAttributeName];
NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc] initWithString:#"Year :" attributes: mediumDict];
UIFont *helNormal = [UIFont fontWithName:#"HelveticaNeue" size:12.0];
NSDictionary *normalDict = [NSDictionary dictionaryWithObject: helNormal forKey:NSFontAttributeName];
NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:#"\n2016" attributes: normalDict];
[mAttrString appendAttributedString:nAttrString];
_yearLabel.attributedText = mAttrString;
[_yearLabel setNumberOfLines:2];
If you want more space around text in UILabel, specify width and height constraints for the same.
I would like to know what's the possible reason?
mAutoCommentLabel.text is nil or cellFont is nil?
UIFont* cellFont = mAutoCommentLabel.font;
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:mAutoCommentLabel.text attributes:#{NSFontAttributeName: cellFont}];
CGRect rect =
[attrStr boundingRectWithSize:CGSizeMake(withWidth, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil];
int commentHeight = rect.size.height;
Crash log from app store heres:
Hello I'm working on chat app in which user can send smiley (emoji) to other user. For text its working perfect but when it contains emoji then it is correct. I have used method for this
- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = width;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:constraintSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:attributesDictionary
context:nil];
CGSize stringSize = frame.size;
return stringSize;
}
and the results are something like
As from images you can see what i want to say and what i need. I need some between two lines when there is emoji text in UILabel. Any help will be appreciated. Thanks in advance.
Edit: I have given answer on this. But if some have some quick hack for this then that will be great for me.
I have made a good work to do it manually and it looks good for me now. I have added linespacing for label text and it works good for me. I have set text like
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#",message_text]];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:4];
[attrString addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, [message_text length])];
lblChatMSG.attributedText = attrString;
and my method to get height of text is
- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = width;
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineSpacing = 4;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:constraintSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:attributesDictionary
context:nil];
CGSize stringSize = frame.size;
return stringSize;
}
And the result is
I just increased the required height of the label and adjusted its origin.
// Expand label to avoid cutoff emojis
label.frame.size.height += 8.0
label.frame.origin.y -= 4.0
Is there an equivalent for NSTextList on iOS?
On OSX i can do
NSTextList *list = [[NSTextList alloc] initWithMarkerFormat:#"square" options:0];
NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraph setTextLists:[NSArray arrayWithObject:list]];
NSTextTab *t1 = [[NSTextTab alloc] initWithType:0 location:11.0f];
NSTextTab *t2 = [[NSTextTab alloc] initWithType:0 location:36.0f];
[paragraph setTabStops:[NSArray arrayWithObjects:t1, t2, nil]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraph, NSParagraphStyleAttributeName, nil];
NSString *string = #"\t▪\tList item 1\n\t▪\tList item 2\n\t▪\tList item 3";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string attributes:attributes];
but there isn't an alternative for iOS, as all the solutions doesn't behave correctly
This question already has answers here:
How can I debug 'unrecognized selector sent to instance' error
(9 answers)
Closed 8 years ago.
I'm displaying an array of dictionaries that comes from web service, in a table. This is the code snippet in cellForRowAtIndexPath.
cell.businessNameLabel.text = [dataDict objectForKey:#"business_name"];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:#"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];
cell.serviceTypeLabel.text = [dataDict objectForKey:#"address"];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:#"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];
cell.businessID = [dataDict objectForKey:#"business_id"];
cell.reviewScoreLabel.text = [dataDict objectForKey:#"rating"];
return cell;
The app crashes on the following line:
cell.reviewScoreLabel.text = [dataDict objectForKey:#"rating"];
With the following exception detail:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xb000000000000003'
The dictionary object data looks something like this:
address = "Boston, MA, United States";
"business_id" = 15;
"business_image" = "http://demo.web.com/home-business/upload/user/1420452418_.jpeg";
"business_name" = autodealersma;
lat = "42.3584865";
lng = "-71.06009699999998";
rating = "4.3";
service = "auto dealers";
I think it is because you are trying to set an NSNumber to a text property. The text property needs a NSString. Could you try:
cell.reviewScoreLabel.text = [NSString stringWithFormat:#"%#",[dataDict objectForKey:#"rating"]];
Modify your code to
cell.businessNameLabel.text = [NSString stringWithFormat:#"%#", [dataDict objectForKey:#"business_name"]];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:#"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];
cell.serviceTypeLabel.text = [NSString stringWithFormat:#"%#",[dataDict objectForKey:#"address"]];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:#"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];
cell.businessID = [dataDict objectForKey:#"business_id"];
cell.reviewScoreLabel.text = [NSString stringWithFormat:#"%#",[dataDict objectForKey:#"rating"]];
return cell;
The reason of the crash must be this, the object you are trying to set to your label may not be a NSString. So better take StringValue or format it.
try this ...
cell.businessNameLabel.text = [[dataDict objectForKey:#"business_name"]objectAtIndex:indexPath.row];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:#"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];
cell.serviceTypeLabel.text = [[dataDict objectForKey:#"address"]objectAtIndex:indexPath.row];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:#"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];
cell.businessID = [[dataDict objectForKey:#"business_id"]objectAtIndex:indexPath.row];
cell.reviewScoreLabel.text = [[dataDict objectForKey:#"rating"]objectAtIndex:indexPath.row];
return cell;