2017-12-05 1 views
-1

레이블의 스크린 샷을 첨부 했으므로 다음 내용을 표시해야합니다. 두 개의 이미지와 흰색 테두리가있는 텍스트로 된 타임 스탬프 텍스트가 추가되었습니다. NSTextAttachment를 사용하여 레이블에 이미지를 추가 한 다음 마지막 또는 두 이미지 사이에 표시해야하는 텍스트를 조건에 따라 추가합니다. 레이블을 하나 더 사용하면 텍스트가 끝날 때마다 쉽게 얻을 수 있지만 쉽지는 않을 것입니다. 레이블의 전체 텍스트를 만들기 위해 많은 수의 튜토리얼을 보았습니다. 특정 부분 만 테두리를 만들 수있는 방법이 있습니까? 레이블 텍스트. enter image description here레이블의 특정 부분 주위에 테두리를 그립니다.

+0

쉽지 않습니다. 'NSAttributedString'에는 이것에 대한 빌드가 없으므로 CoreText를 사용해야 할 수도 있습니다. 최고의 솔루션? "PG-n"에 대한 이미지가 미리 만들어져 이미지를 그린 다음 'NSTextAttachment'를 사용할 수 있고,이 "PG-13"을 "심볼"로 간주하는 특정 글꼴을 사용하거나 만들 수 있습니다 "/ 국경과 숯 ... – Larme

+0

안녕하세요 Larme, 당신은 경계와 텍스트를 이미지로 만들고 레이블에 추가한다고 말합니다. 간단히 설명해 주시겠습니까? – manideep

+0

몇 가지 리드/아이디어 : https://stackoverflow.com/questions/23236703/draw-text-with-a-border-and-background-for-a-nstableview-cell – Larme

답변

1

는 현재이

같은 그

목표 - C 버전 당신은 할 수 있습니다 PG 이미지의 구현이 당신의 필요 텍스트와 이미지를 만들어 NSTextAttachment 같은 텍스트에 연결해야합니다 사용

@implementation ImageHelper 

+(UIImage*)imageForText:(NSString*)text{ 
     UILabel * lblBadge = [[UILabel alloc] initWithFrame:CGRectMake(0,0,16,16)]; 
     [lblBadge setTextAlignment:NSTextAlignmentCenter]; 
     [lblBadge setLineBreakMode:NSLineBreakByClipping]; 
     [lblBadge setTextColor:[UIColor darkGrayColor]]; 
     [lblBadge.layer setCornerRadius:2.0f]; 
     [lblBadge.layer setBorderWidth:1]; 
     [lblBadge setText:text]; 
     [lblBadge sizeToFit]; 
     [lblBadge setBounds:CGRectMake(0,0,lblBadge.bounds.size.width + 4,lblBadge.bounds.size.height)]; 

     UIGraphicsBeginImageContextWithOptions(lblBadge.bounds.size, NO, [UIScreen mainScreen].scale); 
     [lblBadge.layer setAllowsEdgeAntialiasing:YES]; 
     [lblBadge.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return img; 
} 

@end 

예를

NSMutableAttributedString * normalNameString = [[NSMutableAttributedString alloc]initWithString:@"testing "]; 

NSTextAttachment * attachment = [[NSTextAttachment alloc] init]; 
attachment.image = [ImageHelper imageForText:@"PG-13"]; 
attachment.bounds = CGRectMake(0, -6, attachment.image.size.width, attachment.image.size.height); 
[normalNameString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; 

self.lblText.attributedText = normalNameString; 

스위프트 버전

당신은 사용

class imageHelper{ 
    static func pgImage(textValue:String) ->UIImage{ 
     let label = UILabel(frame: CGRect(x: 0, y: 0, width: 15, height: 16)) 
     label.lineBreakMode = .byClipping 
     label.textAlignment = .center 
     label.textColor = UIColor.darkGray 
     label.layer.borderColor = UIColor.darkGray.cgColor 
     label.layer.borderWidth = 1 
     label.layer.cornerRadius = 2 
     label.text = textValue 
     label.sizeToFit() 
     label.bounds = CGRect(x: 0, y: 0, width: label.bounds.size.width + 4, height: label.bounds.size.height) 

     UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, UIScreen.main.scale) 
     label.layer.allowsEdgeAntialiasing = true 
     label.layer.render(in: UIGraphicsGetCurrentContext()!) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image! 
    } 
} 

예처럼 PG 이미지를 만들 수 있습니다

 let normalNameString = NSMutableAttributedString.init(string: "testing ") 

     let attachment = NSTextAttachment() 
     attachment.image = imageHelper.pgImage(textValue: "PG-13") 
     attachment.bounds = CGRect(x: 0, y: -6, width: (attachment.image?.size.width)!, height: (attachment.image?.size.height)!) 
     normalNameString.append(NSAttributedString(attachment: attachment)) 

     self.lblText.attributedText = normalNameString 

결과

enter image description here

관련 문제