2012-04-20 24 views

Trả lời

5

Hãy xem NSAttributedString ... Sử dụng OHAttributedLabel vẽ NSAttributedString vì UILabel, UITextView doesnot hỗ trợ NSAttributedString ...

PS: nếu bạn có kế hoạch để phân phối một iOS6 hoặc ứng dụng mới hơn, như UILabel bây giờ hỗ trợ NSAttributedString, bạn nên sử dụng UILabel trực tiếp thay vì OHAttributedLabel vì nó bây giờ được hỗ trợ bởi hệ điều hành.

+0

có vẻ thú vị !, thx! –

+1

Tôi chưa thử nghiệm, nhưng tôi nghĩ * điều này đã lỗi thời như iOS 6.0. Bản gốc 'UILabel' hiện có thuộc tính' attributedText' - xem http://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html # // apple_ref/occ/instp/UILabel/attributedText –

+0

@MarkAmery Tôi đã cập nhật câu trả lời khác của tôi, nhưng quên cập nhật điều này. Cảm ơn bạn đã thông báo. http://stackoverflow.com/questions/4208282/advance-search-implementation-in-iphone/4208329#4208329 – KingofBliss

1

Điều này là không thể với UILabel. Nhưng bạn có thể sử dụng Core Text.

Một cách tiếp cận dễ dàng hơn có thể là sử dụng UIWebView rất nhỏ. Trong này, bạn có thể thiết lập phông chữ bằng cách sử dụng lệnh html.

1

Im xin lỗi nhưng đó là không thể với việc thực hiện UILabel, bạn có hai lựa chọn:

  • Bạn có thể tạo một UIWebView và đưa các văn bản định dạng HTML
  • Bạn có thể làm cho thực hiện UILabel của riêng bạn với functionallity mà , ví dụ: Core Text
1

Thay vì sử dụng hai UILabels, bạn có thể có một nhãn với nhiều kiểu phông chữ và màu sắc văn bản. Dưới đây là một ví dụ:

UILabel *customLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 25)]; 
customLbl.backgroundColor = [UIColor yellowColor]; 
[self createTwoTextStyleInSingleUILabel:customLbl];// custom method calling 
[self.view addSubview:customLbl]; 

Phương pháp chỉnh đi như thế này: Trước khi áp dụng phương pháp này, thêm QuartzCore khuôn khổ (cần thiết cho CALayers), và khung CoreText (cần thiết cho chuỗi gán.) Trong dự án của bạn.

#import <QuartzCore/QuartzCore.h> 
    #import <CoreText/CoreText.h> 

- (void)createTwoTextStyleInSingleUILabel: (UILabel *) myLabel{ 
NSString *firstText = NSLocalizedString(@"First text:", nil); 
NSString *secondText = [NSString stringWithFormat:@"%@ %@",firstText,@"Second text"]; 
CATextLayer *myLabelTextLayer; 
/* Create the text layer on demand */ 
if (!myLabelTextLayer) { 
    myLabelTextLayer = [[CATextLayer alloc] init]; 
    myLabelTextLayer.backgroundColor = [UIColor clearColor].CGColor; 
    myLabelTextLayer.wrapped = NO; 
    CALayer *layer = myLabel.layer; //assign layer to your UILabel 
    myLabelTextLayer.frame = CGRectMake((layer.bounds.size.width-180)/2 + 10, (layer.bounds.size.height-30)/2 + 10, 180, 30); 
    myLabelTextLayer.contentsScale = [[UIScreen mainScreen] scale]; 
    myLabelTextLayer.alignmentMode = kCAAlignmentCenter; 
    [layer addSublayer:myLabelTextLayer]; 
} 
/* Create the attributes (for the attributed string) */ 
// customizing first string 
CGFloat fontSize = 16; 
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize]; 
CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL); 
CGColorRef cgColor = [UIColor redColor].CGColor; 
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          (__bridge id)ctBoldFont, (id)kCTFontAttributeName, 
          cgColor, (id)kCTForegroundColorAttributeName, nil]; 
CFRelease(ctBoldFont); 
// customizing second string 
UIFont *font = [UIFont systemFontOfSize:16]; 
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL); 
CGColorRef cgSubColor = [UIColor blackColor].CGColor; 
NSDictionary *subAttributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)ctFont, (id)kCTFontAttributeName,cgSubColor, (id)kCTForegroundColorAttributeName, nil]; 
CFRelease(ctFont); 
/* Create the attributed string (text + attributes) */ 
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:secondText attributes:attributes]; 
float lengthOfSecondString = 12.0; // length of second string including blank space inbetween text, space in front , space after text.. Be careful, your app may crash here if length is beyond the second text length (lengthOfSecondString = text length + blank spaces) 
[attrStr addAttributes:subAttributes range:NSMakeRange(firstText.length, lengthOfSecondString)]; 
// you can add another subattribute in the similar way as above , if you want change the third textstring style 
/* Set the attributes string in the text layer :) */ 
myLabelTextLayer.string = attrStr; 
myLabelTextLayer.opacity = 1.0; //to remove blurr effect 

} 
+0

Tôi sẽ kiểm tra mã này, thực sự thx :) –

4

Có một cách để đặt các phông chữ khác nhau/nhiều & các thuộc tính khác trên Nhãn bằng NSMutableAttributedString. Foll là mã của tôi:

UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0]; 
NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];  
NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict]; 

UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0]; 
NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName]; 
NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];  
[VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))]; 

[AattrString appendAttributedString:VattrString]; 


lblText.attributedText = AattrString; 

Lưu ý rằng lblText là UILabel, là chủ sở hữu tệp.
Người ta có thể tiếp tục phụ thêm như nhiều NSMutableAttributedString ông muốn ..

Cũng Lưu ý rằng tôi đã thêm Verdana & phông chữ arial trong dự án của tôi & thêm một plist cho cùng.

+0

không hoạt động .. [NSDictionary dictionaryWithObject: ArialFont forKey: NSFontAttributeName] ; nhận được lỗi trong dòng này –

+0

@RajeshPillai bạn đã xác định phông chữ trong dòng đầu tiên chưa? UIFont * ArialFont = [UIFont fontWithName: @ "arial" size: 18.0]; – Akshay

0

Modified câu trả lời Akshay và thực hiện nó với Swift:

// First part with font size 10 
let firstPartInfo = [NSFontAttributeName: UIFont.systemFontOfSize(10.0)] 
var attributedString = NSMutableAttributedString(string: "First Part", attributes: firstPartInfo) 

// Second part with font size 20 
let secondPartInfo = [NSFontAttributeName: UIFont.systemFontOfSize(20.0)] 
attributedString.appendAttributedString(
    NSAttributedString(string: "Second Part", attributes: secondPartInfo)) 

// Lastly set the attributed text 
label.attributedText = attributedString 

Không có lý do để sử dụng giải pháp của bên thứ ba hoặc các lớp con.

Các vấn đề liên quan