Horizontal spacing for NSTextAttachment - ios

I'm trying to display an image in a UITextField using NSTextAttachment, but I want some horizontal space between the image and the text. However, when add the NSKernAttributeName attribute to the attributed string as follows, it resets the height of the attachment to the same height as the surrounding text.
var str = NSMutableAttributedString(attributedString: NSAttributedString(attachment: imageAttachment))
str.addAttribute(NSKernAttributeName, value: 10, range: NSRange(location: 0,length: 1))
Is there another way to add horizontal space between the image and the text?

The most direct way is in string start set a few space :
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setImage:[UIImage imageNamed:#"dest_poi_content_quotation"]];
NSString *reviewText = [NSString stringWithFormat:#" %#", review.text];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:reviewText];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:attrStringWithImage atIndex:0];
[self.lblComment setAttributedText:attributedString];

Related

Calculate height and number of lines of the label

I have a text that is shown in a UILabel. However, this text have several line spaces as shown below. Now, I want to calculate the height of this label, considering the newline, Bold-text and font size. Since this text can not be placed in a Single line in the label, there might be several lines that we must determine at runtime. Based on this height I want to increase the y cordinate of my UILabel so the UILabel will always be stuck to the bottom of the screen. (Only the height will increase (upwards))
How can I solve this?
NSAttributedString *linespace = [[NSAttributedString alloc] initWithString: #"\n"];
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:#"Mathews is a " attributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Bold" size:40]}];
NSAttributedString* attributed = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:#"%#", #"Bad guy"] attributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Bold" size:40]}];
[mutableString appendAttributedString: linespace];
[mutableString appendAttributedString: attributed];
[mutableString appendAttributedString: linespace];
You can get height by
CGRect rectCountry = [mutableString boundingRectWithSize:(CGSize){#“YOUR WIDTH”, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize sizeCountry = rectCountry.size;

Modifying the width of an underline using attributed text?

Is there a a way to modify the line width of the underline an NSAttributedString?
It seems I can easily modify the color but I can't modify the width of the underline itself easily.
You can set NSUnderlineStyleThick or NSUnderlineStyleSingle, eg:
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Thick underline" attributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleThick)}];
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Normal underline" attributes:#{NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)}];
Full list of underline styles here: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/index.html#//apple_ref/c/tdef/NSUnderlineStyle
// assume Label name as "label"
// underline code
CGSize expectedLabelSize = [#"Some text" sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewForUnderline=[[UIView alloc] init];
viewForUnderline.frame=CGRectMake((label.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (label.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewForUnderline.backgroundColor=[UIColor whiteColor];
[self.view addSubview:viewForUnderline];
or you can use following line of code
label.attributedText = [[NSAttributedString alloc] initWithString:#"Some Text"
attributes:underlineAttribute];

Chinese characters interline spacing in UITextView

I have translated an app into Simplified Chinese.
The app is using a UITextView and the contents of it is Simplified Chinese text.
I have noticed that (sometimes not always) the interline spacing is wrong, the lines are practically touching each other :
I am setting the contents of the UITextview via an attributed string that I create based on a required pointsize like this:
-(NSMutableAttributedString*) textWithPointSize:(CGFloat)pointSize
{
UIFont * myFontDescr = [UIFont systemFontOfSize:pointSize];
NSMutableAttributedString * description = [[NSMutableAttributedString alloc] initWithString:self.exercise.descr attributes:#{NSFontAttributeName:myFontDescr}];
UIFont * myFontTips = [UIFont italicSystemFontOfSize:pointSize];
NSMutableAttributedString * tips = [[NSMutableAttributedString alloc] initWithString:self.exercise.tips attributes:#{NSFontAttributeName:myFontTips}];
NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithAttributedString:description];
[text appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n\n"]];
[text appendAttributedString:tips];
return text;
}
I am using the systemFontOfSize: function, I guess this should handle Chinese characters correctly? Any ideas why this could be happening?
I did not find out why but I implemented a work around by explicitly setting the interline spacing (only when language is Chinese) like this:
//for chinese language we have detected a problem with the interline spacing
//it does happen randomly, and so to workaround this we set the linespacing explicitly
if ( [NSLocalizedString(#"__CurrentLanguage",#"zh-Hans") isEqualToString:#"zh-Hans"] ) {
//make paragraph styl with interline spacing
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 0.25 * pointSize;
//font for description
UIFont * myFontDescr = [UIFont systemFontOfSize:pointSize];
NSMutableAttributedString * description = [[NSMutableAttributedString alloc] initWithString:self.exercise.descr attributes:#{NSFontAttributeName:myFontDescr, NSParagraphStyleAttributeName:paragraphStyle}];
//font for tips
UIFont * myFontTips = [UIFont italicSystemFontOfSize:pointSize];
NSMutableAttributedString * tips = [[NSMutableAttributedString alloc] initWithString:self.exercise.tips attributes:#{NSFontAttributeName:myFontTips,NSParagraphStyleAttributeName:paragraphStyle}];
//add description then...
NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithAttributedString:description];
//concat the tips (with 2x newline in between)
[text appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n\n"]];
[text appendAttributedString:tips];
//return value
return text;
}
//its not chinese, so do "normal" stuff

How could you make a uilabel wrap around an image (like shown)

How could you achieve this effect:
Maybe some sort of NSAtributedString?
I have thought of hacky ways to just add spaces, but it needs to do it dynamically based on the width of the image.
Any ideas?
NOTE happily you can do this very easily with UITextView:
https://stackoverflow.com/a/20033752/294884
this question is about UILabel.
Add image in your label with text as below code:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Here is some text that is wrapping around like an image"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:#"first.jpg"];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString insertAttributedString:attrStringWithImage atIndex:0];
[_lbn setAttributedText:attributedString];
Your OUTPUT :
You can use an NSAttributedString with an NSTextAttachment
NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
[attachment setImage:<#UIImage#>];
NSAttributedString* icon = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:icon atIndex:<#index#>];
but it will only insert the image on one line, so it wont have multiple lines down the side of it (like a newspaper article), so its only useful for small images that fit on one line (sort of like how you have it in your question i guess)

UITextView Text size is auto resized while using the NSAttributedString

In the UITextView text,
before highlight the text, its shown the text size as 16. (in the first image)
after the using the NSAttributedString to highlight. highlighted text only show that same size(16) remaining strings show lesser than that the original size (second .
this problem only in the iPod and iPhone deceive . iPad is shown correct format.
code to highlight the UITextView Text
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIFont fontWithName:#"Arial" size:currentTextSize] forKey:NSFontAttributeName];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:txtview4disp.text];
int startind=[[dict objectForKey:#"BeginIndexPos"]integerValue];
int endind=[[dict objectForKey:#"EndIndexPos"]integerValue];
NSRange rang;
if (startind>=STARTINDEX&&startind<=ENDINDEX) {
if (endind>ENDINDEX) {
int endind2=endind-ENDINDEX;
rang=NSMakeRange(startind-STARTINDEX, (endind-startind)-endind2);
}
else
rang=NSMakeRange(startind-STARTINDEX, endind-startind);
[attrString addAttributes:attrsDictionary range:rang];
[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:rang];
txtview4disp.attributedText=attrString;
It looks like you skipped the original attributes of your UITextView.
Try replacing this line:
NSMutableAttributedString *attrString =
[[NSMutableAttributedString alloc] initWithString:txtview4disp.text];
with following:
NSMutableAttributedString *attrString =
[[NSMutableAttributedString alloc] initWithAttributedString:txtview4disp.attributedText];

Resources