zoukankan      html  css  js  c++  java
  • UI控件

    用swift实现UILabel中的文字居上,居中,居下:

     

    – textRectForBounds:limitedToNumberOfLines:

    用来改变label里面文字展示窗口的大小,你可以自己根据文字的多少,来计算窗口的大小

     

    – drawTextInRect:

    在绘图环境实现文字的绘制,这个方法里面里面已经配置好了绘图环境,使用方式如下:

    1.直接获得当前绘图上下文,

    2.接着更改绘图环境设置

    3.在就是调用super方法来绘制即可

    自定义一个label:

    enum VerticalAlignment {

        

        case VerticalAlignmentTop     

        

        case VerticalAlignmentMiddle    // default

        

        case VerticalAlignmentBottom

    }

     

    class Mylabel: UILabel {

        

        var _verticalAlignment: VerticalAlignment?

        private var verticalAlignment: VerticalAlignment {

            get {

                return _verticalAlignment!

            }

     

            set {

                

                self.verticalAlignment = _verticalAlignment!

                self.setNeedsDisplay()

                

            }

        }

        

     

        override init(frame: CGRect) {

            

            super.init(frame: frame)

            _verticalAlignment = .VerticalAlignmentMiddle

        }

     

        required init?(coder aDecoder: NSCoder) {

            fatalError("init(coder:) has not been implemented")

        }

        //重绘文字的位置,在次方法哪2可以获取文字和label的默认大小,通过改变相对位置来实现。    

        override func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {

            var textRect = super.textRectForBounds(bounds, limitedToNumberOfLines: numberOfLines)

     

            switch verticalAlignment {

                

            case .VerticalAlignmentTop:

                textRect.origin.y = bounds.origin.y

                

            case .VerticalAlignmentBottom:

                textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height

                

            case .VerticalAlignmentMiddle:

                textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0

            }

            return textRect

        }

    //在这里实现重绘,实现真正意义上的重布局

        override func drawTextInRect(rect: CGRect) {

            let actualRect = self.textRectForBounds(rect, limitedToNumberOfLines: self.numberOfLines)

            super.drawTextInRect(actualRect)

        }

        

    }

     

    调用

        let lb = Mylabel()

            self.view.addSubview(safeAreaLabel!)

            lb._verticalAlignment = .VerticalAlignmentTop

      自己在使用时设置了一个值

        lb.opaque = true

          查下资料了解到该属性为BOOL值,UIView的默认值是YES,但UIButton等子类的默认值都是NO。

      

    显示器中的每个像素点都可以显示一个由RGBA颜色空间组成的色值,假如有红色和绿色两个图层色块,对于没有交叉的部分,即纯红色和绿色部分来说,对应位置的像素点只需要简单的显示红或绿,对应的RGBA为(1,0,0,1)和(0,1,0,1)就行了,负责图形显示的GPU需要很小的计算量就可以确定像素点对应的显示内容。

    问题是红色和绿色还有相交的一块,其相交的颜色为黄色。这里的黄色是怎么来的呢?原来,GPU会通过图层一和图层二的颜色进行图层混合,计算出混合部分的颜色,最理想情况的计算公式如下:

    R = S + D * ( 1 – Sa )

    其中,R表示混合结果的颜色,S是源颜色(位于上层的红色图层一),D是目标颜色(位于下层的绿色图层二),Sa是源颜色的alpha值,即透明度。公式中所有的S和D颜色都假定已经预先乘以了他们的透明度。

    知道图层混合的基本原理以后,再回到正题说说opaque属性的作用。当UIView的opaque属性被设为YES以后,按照上面的公式,也就是Sa的值为1,这个时候公式就变成了:

    R = S

    即不管D为什么,结果都一样。因此GPU将不会做任何的计算合成,不需要考虑它下方的任何东西(因为都被它遮挡住了),而是简单从这个层拷贝。这节省了GPU相当大的工作量。由此看来,opaque属性的真实用处是给绘图系统提供一个性能优化开关!

    按照前面的逻辑,当opaque属性被设为YES时,GPU就不会再利用图层颜色合成公式去合成真正的色值。因此,如果opaque被设置成YES,而对应UIView的alpha属性不为1.0的时候,就会有不可预料的情况发生,这一点苹果在官方文档中有明确的说明:

    An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content,the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.

    大家切记!!!

     

    scrollview:UIScrollView的子控件添加约束与普通view不同,仅仅这4个约束不足以满足它的需求.需要6个约束;

    这是因为,scrollView需要根据添加在其内部的子控件的宽高及与四周的距离计算出它的contentSize.

  • 相关阅读:
    5.Spring常用注解
    4.@Autowired注解与@Resource注解的区别
    3.只读事务@Transactional(readOnly = true)
    2.Object...param参数
    1.private static final long serialVersionUID = 1L
    SOCKET是调用操作系统通信服务的一种机制
    HTTP头和网页分离方法

    Python split()方法
    socket (计算机专业术语)
  • 原文地址:https://www.cnblogs.com/hazhede/p/5474140.html
Copyright © 2011-2022 走看看