【Swift-UIKit】UILabel

本文深入探讨了UILabel中的阴影与偏移属性,包括阴影颜色、阴影偏移量的概念及使用方法,并通过示例展示如何应用这些属性实现文本的视觉效果优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import Foundation
import UIKit

//
//  UILabel.h
//  UIKit
//
//  Copyright (c) 2006-2014 Apple Inc. All rights reserved.
//

class UILabel : UIView, NSCoding {
    
    var text: String! // default is nil
    var font: UIFont! // default is nil (system font 17 plain)
    var textColor: UIColor! // default is nil (text draws black)
    var shadowColor: UIColor! // default is nil (no shadow)
    var shadowOffset: CGSize // default is CGSizeMake(0, -1) -- a top shadow
    var textAlignment: NSTextAlignment // default is NSTextAlignmentLeft
    var lineBreakMode: NSLineBreakMode // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text
    
    // the underlying attributed string drawn by the label, if set, the label ignores the properties above.
    var attributedText: NSAttributedString! // default is nil
    
    // the 'highlight' property is used by subclasses for such things as pressed states. it's useful to make it part of the base class as a user property
    
    var highlightedTextColor: UIColor! // default is nil
    var highlighted: Bool // default is NO
    
    var userInteractionEnabled: Bool // default is NO
    var enabled: Bool // default is YES. changes how the label is drawn
    
    // this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit
    // if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be
    // truncated using the line break mode.
    
    var numberOfLines: Int
    
    // these next 3 property allow the label to be autosized to fit a certain width by scaling the font size(s) by a scaling factor >= the minimum scaling factor
    // and to specify how the text baseline moves when it needs to shrink the font.
    
    var adjustsFontSizeToFitWidth: Bool // default is NO
    var adjustsLetterSpacingToFitWidth: Bool // deprecated - hand tune by using NSKernAttributeName to affect tracking
    var minimumFontSize: CGFloat // NOTE: deprecated - use minimumScaleFactor. default is 0.0
    var baselineAdjustment: UIBaselineAdjustment // default is UIBaselineAdjustmentAlignBaselines
    var minimumScaleFactor: CGFloat // default is 0.0
    
    // override points. can adjust rect before calling super.
    // label has default content mode of UIViewContentModeRedraw
    
    func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect
    func drawTextInRect(rect: CGRect)
    
    // Support for constraint-based layout (auto layout)
    // If nonzero, this is used when determining -intrinsicContentSize for multiline labels
    var preferredMaxLayoutWidth: CGFloat
}

1. shadowColor   shadowOffset
shadowColor :阴影颜色
shadowOffset :阴影偏移量 CGSize类型,默认CGSizeMake(0, -1),左右不偏移,向上偏移一个单位。
CGSizeMake(x,y) x为正向右偏移,为负向左偏移;y为正向下偏移,为负向上偏移。
下图为CGSizeMake(2,5)的效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值