2016-01-31 20 views
8

Tôi muốn tăng độ chênh lệch của bóng trong nhãn UILabel nhưng tôi không thể tìm thấy thuộc tính để làm điều đó. Tôi đã thêm ảnh bên dưới để minh họa sự cố.iOS - Bóng trải trên UILabel

An example of the desired label shadow, and the current one.

Các Nhãn hàng đầu là hiệu quả mong muốn tôi có thể tạo ra trong Photoshop. Nhãn dưới minh họa các thuộc tính tôi có thể tìm thấy trong iOS. Đây là mã tôi đã sử dụng cho Nhãn dưới cùng.

let bottomLabel = UILabel(frame: CGRectMake(0, 0, maxWidth, 18)) 
bottomLabel.backgroundColor = UIColor.clearColor() 
bottomLabel.textColor = UIColor.whiteColor() 
bottomLabel.font = UIFont.boldSystemFontOfSize(16) 
bottomLabel.text = title 
bottomLabel.textAlignment = .Center 
bottomLabel.numberOfLines = 1 
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0) 
bottomLabel.layer.shadowOpacity = 1 
bottomLabel.layer.shadowRadius = 2 

Tôi đã tìm đề xuất sử dụng nhãn thứ hai làm bóng nhưng điều này không cho kết quả mong muốn. Đây là mã cho Nhãn đó.

let bottomLabelShadow = UILabel(frame: CGRectMake(0, 1, maxWidth, 18)) 
bottomLabelShadow.backgroundColor = UIColor.clearColor() 
bottomLabelShadow.textColor = UIColor.blackColor() 
bottomLabelShadow.font = UIFont.boldSystemFontOfSize(16) 
bottomLabelShadow.text = title 
bottomLabelShadow.textAlignment = .Center 
bottomLabelShadow.numberOfLines = 1 
bottomLabelShadow.layer.shadowOffset = CGSize(width: 0, height: 0) 
bottomLabelShadow.layer.shadowOpacity = 1 
bottomLabelShadow.layer.shadowRadius = 2 
+0

Trên thực tế có một bài viết tốt về bóng https://makeapppie.com/2015/05/19/swift-swift-how-to-make-a-drop-shadow-in-user-interfaces/ –

Trả lời

22

Có vẻ như tôi làm việc ở sân chơi. Bạn chỉ cần tăng bán kính bóng để làm cho nó xuất hiện như bạn muốn.

Đây là mã sân chơi chính xác tôi đã sử dụng

let view = UIView(frame: CGRectMake(0,0,100,20)) 
view.backgroundColor = UIColor.yellowColor() 

let bottomLabel = UILabel(frame: CGRectMake(0, 0, 100, 20)) 
bottomLabel.backgroundColor = UIColor.clearColor() 
bottomLabel.textColor = UIColor.whiteColor() 
bottomLabel.font = UIFont.boldSystemFontOfSize(18) 
bottomLabel.text = "Testing" 
bottomLabel.textAlignment = .Center 
bottomLabel.numberOfLines = 1 
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0) 
bottomLabel.layer.shadowOpacity = 1 
bottomLabel.layer.shadowRadius = 6 

view.addSubview(bottomLabel) 

Hoặc nếu bạn đang sử dụng nó trên một nền tảng quan điểm mờ, bạn có thể sử dụng sống động để đạt được một hiệu ứng nhìn tốt hơn.

enter image description here

+0

darnit, bạn đánh tôi với nó. LOL. đã cố gắng tìm cách sử dụng sân chơi sau khi không sử dụng nó quá lâu ... – DeveloperACE

+0

Cảm ơn bạn đã phản hồi nhanh! Tôi nhìn vào nó nhưng nó không phải là kết quả tôi đang tìm kiếm. Tôi đã cập nhật hình ảnh để phù hợp hơn với ví dụ về Sân chơi của bạn. –

+0

cảm ơn vì điều này! – CaffeineShots

1

Rikola của câu trả lời như Swift 3:

import UIKit 

let view = UIView(frame: CGRect.init(x: 0, y: 0, width: 100, height: 20)) 
view.backgroundColor = UIColor.yellow 

let bottomLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 100, height: 20)) 
bottomLabel.backgroundColor = UIColor.clear 
bottomLabel.textColor = UIColor.white 
bottomLabel.font = UIFont.boldSystemFont(ofSize: 18) 
bottomLabel.text = "Testing" 
bottomLabel.textAlignment = .center 
bottomLabel.numberOfLines = 1 
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0) 
bottomLabel.layer.shadowOpacity = 1 
bottomLabel.layer.shadowRadius = 6 

view.addSubview(bottomLabel) 

Nhấn chút "mắt" khi hút vào qua view.addSubview(bottomLabel) in trong thanh đúng, cho phần trình bày trực quan của nhãn.

4
//Try This! Hope this helps!! 

// 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 = [ NSShadowAttributeName: myShadow ] 

// Add the attribute to the string 
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute) 

// set the attributed text on a label 
myLabel.attributedText = myAttrString 
Các vấn đề liên quan