在UITextView中为NSString添加外部描边

4
我正在尝试给文本添加描边并在UITextView中显示。
这就是我想要的。
在图片(由Photoshop创建)中,使用了相同的字体,第一个具有“OUTSIDE”位置,底部文本具有“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];

我正在实现内部效果。如何将位置设置为外部?

1
也许可以使用 NSStrokeWidthAttributeName: [NSNumber numberWithFloat:+3.0], - Larme
1个回答

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中按照您想要的方式进行绘制。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接