以编程方式在视图中显示TextField:更改颜色,字体并移动标签

时间:2018-11-15 11:00:51

标签: swift text fonts colors contenteditable

我有两个collectionView,其中有颜色,其他有字体。我有一个白色的视图,在其中插入了文本视图。 我需要在两件事上向您寻求帮助。

我必须确保当我点击一种颜色或字体时,我会自动编辑在TextView中输入的文本。 我还需要能够移动文本视图,也就是可以根据需要在文本视图中移动它,就像它是一个贴纸一样。

从某种意义上说,我现在希望所有字体都具有“帮助”,但我也应该为每种字体写不同的文字。

您能告诉我如何在标签上附加各种颜色和字体吗?谢谢您的帮助

import UIKit
import Constrainable
protocol FontDelegate: class {
    func sincronizeScroll(indexPath: IndexPath)
}
class CollectionFont: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    weak var delegate: FontDelegate?
    var collectionView: UICollectionView?
    let cellSpacing:CGFloat = 1
    let indexPath: IndexPath
    var datasource: [UIFont] = []
    var imageArray: Array<UIImageView> = []
    var onceOnly = false

    init(datasource: [UIFont], index: IndexPath = [0,0]) {
        self.indexPath = index
        self.datasource = Theme.CustomFontsFamily.customFonts
        super.init(nibName: nil, bundle: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.delegate = self
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewLayout())
        collectionView!.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(collectionView!)
        collectionView!.activate(constraint(edgesTo: self.view))
        collectionView!.backgroundColor = Theme.Colors.sky
        let collectionViewFlowLayout = UICollectionViewFlowLayout()
        collectionView!.setCollectionViewLayout(collectionViewFlowLayout, animated: true)
        collectionViewFlowLayout.scrollDirection = .horizontal
        collectionViewFlowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        collectionViewFlowLayout.minimumLineSpacing = 0
        collectionViewFlowLayout.minimumInteritemSpacing = 0
        collectionView!.register(FontCell.self, forCellWithReuseIdentifier: FontCell.reuseIdentifier)
        collectionView!.delegate = self
        collectionView!.dataSource = self
        collectionView!.isPagingEnabled = true
        collectionView!.bounces = false
        self.collectionView?.reloadData()
    }

    class FontRound {
        let font: UILabel

        init(font: UILabel) {
            self.font = font
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.datasource.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FontCell.reuseIdentifier, for: indexPath) as! FontCell
        cell.array.font = self.datasource[indexPath.row]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = UIScreen.main.bounds.size.width/4
        let height = UIScreen.main.bounds.size.width/4
        return CGSize(width: width, height: height)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        debugPrint("ciaooooo")
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        var visibleRect = CGRect()
        visibleRect.origin = collectionView!.contentOffset
        visibleRect.size = collectionView!.bounds.size
        let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
        guard let indexPath = collectionView!.indexPathForItem(at: visiblePoint) else { return }
        print(indexPath[1])
        self.delegate?.sincronizeScroll(indexPath: indexPath)
    }

    internal func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if !onceOnly {
            collectionView.scrollToItem(at: self.indexPath, at: UICollectionView.ScrollPosition.right, animated: false)
            onceOnly = true
        }
    }
}

final class CollectionFontView: UICollectionView {

    init(_ collectionViewLayout: UICollectionViewLayout) {
        super.init(frame: .zero, collectionViewLayout: collectionViewLayout)
        self.backgroundColor = UIColor.red
        self.showsHorizontalScrollIndicator = false
        register(FontCell.self, forCellWithReuseIdentifier: FontCell.reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



import UIKit
import Foundation
import Constrainable

class FontCell: UICollectionViewCell {
    static let reuseIdentifier = "FontCell_RID"

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    var cellID: String?

    var array = UILabel().then{
        $0.text = "help"
        $0.font = UIFont(name:"Jellee-Roman",size:15)
        $0.contentMode = UIView.ContentMode.scaleAspectFit
        $0.layer.cornerRadius = (((UIScreen.main.bounds.size.width/8)*0.8)/2)
    }

    func setupViews(){
        self.addSubview(self.array)
        self.array.activate([
            array.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.8),
            array.heightAnchor.constraint(equalTo: self.array.widthAnchor),
            array.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            array.centerYAnchor.constraint(equalTo: self.centerYAnchor),
            ])
    }

    func setContents(container: UILabel) {
        for sv in self.contentView.subviews {
            sv.removeFromSuperview()
        }
        self.contentView.addSubview(container)
        container.activate(constraint(edgesTo: self.contentView))
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

标签的示例图片:

0 个答案:

没有答案