2016-01-18 29 views
3

我正在嘗試將一個筆劃添加到文本並在UITextView中顯示。在UITextView中將外部筆觸添加到NSString中

這是我想要的。

圖片中(CREATED FROM PHOTOSHOP)使用了相同的字體,第一個使用了Postion "OUTSIDE",底部文本使用了position INSIDE

enter image description here

我使用下面的代碼在Objective-C

NSShadow * shadow = [[NSShadow alloc] init]; 
    shadow.shadowColor = [UIColor blackColor]; 
    shadow.shadowOffset = CGSizeMake(0, 0); 

    NSDictionary * textAttributes = 
    @{ NSForegroundColorAttributeName : [UIColor whiteColor], 
     NSShadowAttributeName   : shadow, 
     NSStrokeColorAttributeName  : [UIColor blackColor], 
     NSStrokeWidthAttributeName  : [NSNumber numberWithFloat:-3.0], 
     NSFontAttributeName   : [UIFont fontWithName:@"UbuntuCondensed-Regular" size:40] }; 

    textTitleResult.attributedText = [[NSAttributedString alloc] initWithString:textTitle.text 
                   attributes:textAttributes]; 

我實現INSIDE效果。我怎麼能設置位置到外面

+1

也許''NSStrokeWidthAttributeName:[NSNumber numberWithFloat:+3.0],'? – Larme

回答

1

我不認爲有一個屬性,可以做到你想要的。但是,我有一個想法,可能會爲您提供一種「僞造」它的方法。

您可以在原始位置的確切位置添加另一個標籤。新標籤的筆畫大小隻有筆畫的一半,但筆畫顏色與文字顏色相同。由於筆畫「坐在」邊框(文字內半部分,外半部分),它會阻止原始筆畫的一半,使其看起來好像在外面。

未做代碼太漂亮了,它會是這個樣子:

UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)]; 
infoLabel.textAlignment = NSTextAlignmentCenter; 
[self.view addSubview:infoLabel]; 

UILabel *secondInfoLabel = [[UILabel alloc] initWithFrame:infoLabel.frame]; 
    secondInfoLabel.textAlignment = NSTextAlignmentCenter; 
[self.view addSubview:secondInfoLabel]; 

NSDictionary * secondTextAttributes = 
@{ NSForegroundColorAttributeName : [UIColor whiteColor], 
    NSStrokeColorAttributeName  : [UIColor whiteColor], 
    NSStrokeWidthAttributeName  : [NSNumber numberWithFloat:-3.0], 
    NSFontAttributeName   : [UIFont systemFontOfSize:40] }; 

secondInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Welcome To" attributes:secondTextAttributes]; 

NSShadow * shadow = [[NSShadow alloc] init]; 
shadow.shadowColor = [UIColor blackColor]; 
shadow.shadowOffset = CGSizeMake(0, 0); 
shadow.shadowBlurRadius = 2; 

NSDictionary * textAttributes = 
@{ NSForegroundColorAttributeName : [UIColor whiteColor], 
    NSShadowAttributeName   : shadow, 
    NSStrokeColorAttributeName  : [UIColor blackColor], 
    NSStrokeWidthAttributeName  : [NSNumber numberWithFloat:-6.0], 
    NSFontAttributeName   : [UIFont systemFontOfSize:40] }; 

infoLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Welcome To" attributes:textAttributes]; 

另一種選擇是創建自己的自定義視圖,並自己繪製它只是你想要的方式(在drawRect)。