ioslabel阴影_ios-如何将文本阴影应用于UITextView?

ios-如何将文本阴影应用于UITextView?

其实我爱UILabel。它们很甜。 现在,我必须转到UITextView,因为UILabel没有将文本垂直对齐到顶部。 该死的。 我真正需要的一件事是文本阴影。 UILabel拥有它。 UITextView似乎没有它。 但是我想那家伙只是使用相同的底层UIKit NSString加法? 也许有人已经解决了这个问题? 我会覆盖什么?

7个解决方案

154 votes

text.layer.shadowColor = [[UIColor whiteColor] CGColor];

text.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);

text.layer.shadowOpacity = 1.0f;

text.layer.shadowRadius = 1.0f;

并且不要忘记加顶:

#import

adjwilli answered 2020-07-18T14:06:29Z

15 votes

答案与

text.layer.shadowColor

在整个textView中添加阴影,也许可以,但是它不仅在文本中添加阴影。

正确答案是:

CALayer *textLayer = ((CALayer *)[textView.layer.sublayers objectAtIndex:0]);

textLayer.shadowColor = [UIColor whiteColor].CGColor;

textLayer.shadowOffset = CGSizeMake(0.0f, 1.0f);

textLayer.shadowOpacity = 1.0f;

textLayer.shadowRadius = 1.0f;

Nikita answered 2020-07-18T14:07:03Z

12 votes

迅捷3

let textView = UITextView(frame: view.frame)

textView.font = UIFont(name: "Helvetica", size: 64.0)

textView.textColor = .red

textView.text = "Hello World"

textView.layer.shadowColor = UIColor.black.cgColor

textView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)

textView.layer.shadowOpacity = 1.0

textView.layer.shadowRadius = 2.0

textView.layer.backgroundColor = UIColor.clear.cgColor

斯威夫特2.3

let textView = UITextView(frame: view.frame)

textView.font = UIFont(name: "Helvetica", size: 64.0)

textView.textColor = UIColor.redColor()

textView.text = "Hello World"

textView.layer.shadowColor = UIColor.blackColor().CGColor

textView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)

textView.layer.shadowOpacity = 1.0

textView.layer.shadowRadius = 2.0

textView.layer.backgroundColor = UIColor.clearColor().CGColor

Leo Dabus answered 2020-07-18T14:07:27Z

9 votes

这个Swift示例使用属性阴影方法在文本上添加阴影。 有关更多有关Swift中属性字符串的信息,请参见此答案。 如果需要,此方法(与使用layer方法相反)使您可以灵活地在一定范围的文本上设置阴影。

// Create a string

let myString = "Shadow"

// Create a shadow

let myShadow = NSShadow()

myShadow.shadowBlurRadius = 3

myShadow.shadowOffset = CGSize(width: 3, height: 3)

myShadow.shadowColor = UIColor.gray

// Create an attribute from the shadow

let myAttribute = [ NSAttributedStringKey.shadow: myShadow ]

// Add the attribute to the string

let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)

// set the attributed text on a label

myLabel.attributedText = myAttrString // can also use with UITextView

为Swift 4更新

Suragch answered 2020-07-18T14:07:52Z

8 votes

在iOS 6+中使用属性文本

NSShadow * shadow = [[NSShadow alloc] init];

shadow.shadowColor = [UIColor blackColor];

shadow.shadowOffset = CGSizeMake(2, 2);

NSDictionary * textAttributes =

@{ NSForegroundColorAttributeName : [UIColor blueColor],

NSShadowAttributeName : shadow,

NSFontAttributeName : [UIFont boldSystemFontOfSize:20] };

textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello"

attributes:textAttributes];

Robert answered 2020-07-18T14:08:12Z

0 votes

这取决于您使用的iOS版本。 从iOS 6开始,支持单个阴影,您可以将其设置为NSAttributedString上的属性,然后在标签上进行设置。

Cocoanetics answered 2020-07-18T14:08:32Z

0 votes

迅捷4,迅捷4.2,迅捷5及以上简单优雅的解决方案,可以从界面生成器轻松使用

extension UIView {

/* The color of the shadow. Defaults to opaque black. Colors created

* from patterns are currently NOT supported. Animatable. */

@IBInspectable var shadowColor: UIColor? {

set {

layer.shadowColor = newValue!.cgColor

}

get {

if let color = layer.shadowColor {

return UIColor(cgColor: color)

}

else {

return nil

}

}

}

/* The opacity of the shadow. Defaults to 0.4 Specifying a value outside the

* [0,1] range will give undefined results. Animatable. */

@IBInspectable var shadowOpacity: Float {

set {

layer.shadowOpacity = newValue

}

get {

return layer.shadowOpacity

}

}

/* The shadow offset. Defaults to (1, 2). Animatable. */

@IBInspectable var shadowOffset: CGPoint {

set {

layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)

}

get {

return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)

}

}

/* The blur radius used to create the shadow. Defaults to 3. Animatable. */

@IBInspectable var shadowRadius: CGFloat {

set {

layer.shadowRadius = newValue

}

get {

return layer.shadowRadius

}

}

}

midhun p answered 2020-07-18T14:08:54Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值