2016-01-18 2 views
3

Я пытаюсь добавить штрих к тексту и показать в UITextView.Добавление внешнего строя в NSString в UITextView

Вот что я хочу.

На картинке (СОЗДАН ИЗ 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]; 

Я ВНУТРИ достижения эффекта. Как установить позицию в OUTSIDE

+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).

 Смежные вопросы

  • Нет связанных вопросов^_^