Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting vertical align of truncated tails for NSAttributedString with NSTextAttachment

I'm using the following code to generate a NSAttributedString for UILabel in iOS 8.

// a long long Chinese title 
NSString *title = @"这是一个很长很长很长很长很长很长的中文标题";
// setup icon attachment
NSTextAttachment *iconAttachment = [[NSTextAttachment alloc] init];
iconAttachment.image = [UIImage imageNamed:imageName];
iconAttachment.bounds = bounds;
NSAttributedString *ycardImageString = [NSAttributedString attributedStringWithAttachment:iconAttachment];

// setup attributed text
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:title];
if (shouldShowYcard) {
    [attributedText insertAttributedString:ycardImageString atIndex:0];
    [attributedText insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1];
    [attributedText addAttribute:NSBaselineOffsetAttributeName value:@(offset) range:NSMakeRange(0, 1)];
}
NSRange titleRange = NSMakeRange(shouldShowYcard ? 2 : 0, title.length);
[attributedText addAttribute:NSFontAttributeName value:font range:titleRange];
[attributedText addAttribute:NSForegroundColorAttributeName value:color range:titleRange];

However it seems that the NSTextAttachment will effect the vertical position of truncated tails, just like the following pictures.

NSAttributedString with NSTextAttachment NSAttributedString without NSTextAttachment NSAttributedString with NSTextAttachment In English NSAttributedString without NSTextAttachment In English

Is there a way to set the vertical aignment for the truncated tails?

My goal is to have bottom align tails in Chinese language.

This is an icon for test.icon

like image 781
xi.lin Avatar asked Sep 22 '16 15:09

xi.lin


2 Answers

Try this One

   UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 300, 30)];
    [self.view addSubview:lbl];
    NSString *t= @"这是一个很长很长很长很长很长很长的中文标题漢字";
    NSTextAttachment *iconatt = [[NSTextAttachment alloc]init];
    iconatt.image = [UIImage imageNamed:@"phnzY.png"];
    iconatt.bounds = CGRectMake(0, 0, 44, 22);

    NSAttributedString *ycardstring = [NSAttributedString attributedStringWithAttachment:iconatt];
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:t];
    [attributedText insertAttributedString:ycardstring atIndex:0];
    [attributedText insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1];
     [attributedText addAttribute:NSBaselineOffsetAttributeName value:@(0.0) range:NSMakeRange(0, 1)];
        NSRange titleRange = NSMakeRange( 0 , t.length);
    [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22.0] range:titleRange];
    [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:titleRange];
    lbl.attributedText = attributedText;

It will Give output Like this

output

like image 188
Jitendra Modi Avatar answered Nov 09 '22 03:11

Jitendra Modi


You can try to use NSMutableParagraphStyle to set the paragraphStyle. Here is the Code:

    let label1 = UILabel(frame: CGRect(x: 50, y: 50, width: 100, height: 30))
    let label2 = UILabel(frame: CGRect(x: 50, y: 90, width: 100, height: 30))
    view.addSubview(label1)
    view.addSubview(label2)
    let t = "12341421dsadyusatdiuwtquyidtywatyudigsaydgsadysaghdkgaugduiyyudgasgdj"
    let iconAttachment = NSTextAttachment()
    iconAttachment.image = UIImage(named: "caseEditorBtn0")
    iconAttachment.bounds = CGRect(x: 0, y: 0, width: 20, height: 30);
    let ycardImageString = NSAttributedString(attachment: iconAttachment)
    let yT0 = NSMutableAttributedString(string: t)
    let yT1 = NSMutableAttributedString(string: t)

    let p = NSMutableParagraphStyle()
    p.lineBreakMode = .byTruncatingTail //set line break model
    p.alignment = .right                //set aligment
    yT1.setAttributes([NSParagraphStyleAttributeName: p], range: NSRange(location: 0, length: yT1.length))
    yT0.setAttributes([NSParagraphStyleAttributeName: p], range: NSRange(location: 0, length: yT0.length))
    //insert you custom text attachment at last, or it may be not show
    yT0.insert(ycardImageString, at: 0) 

    label1.attributedText =  yT0
    label2.attributedText = yT1
like image 36
Hao Avatar answered Nov 09 '22 02:11

Hao