gpt4 book ai didi

ios - 在 UITextView 中向 NSString 添加外部描边

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:57 27 4
gpt4 key购买 nike

我正在尝试向文本添加笔划并在 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];

我正在实现 INSIDE 效果。如何将位置设置为 OUTSIDE

最佳答案

我不认为有一种属性可以在您需要时准确执行。但是,我有一个想法可能会为您提供一种“伪造”它的方法。

您可以在原始标签的确切位置添加另一个标签。新标签的大小将是笔划的一半,但笔划颜色将与文本颜色相同。由于笔划“位于”边框上(一半在文本内部,一半在文本外部),它会挡住原始笔划的一半,使其看起来好像在外面。

在不让代码太漂亮的情况下,它看起来像这样:

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];

另一种选择是创建您自己的自定义 View 并按照您想要的方式自行绘制它(在 drawRect 中)。

关于ios - 在 UITextView 中向 NSString 添加外部描边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34859500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com