Câu hỏi
Tôi đang cố gắng tạo thành phần UIView tùy chỉnh trong sân chơi nhanh. Tôi đã có thể tìm ra cách để làm mọi thứ ngoại trừ việc hiển thị văn bản chính xác. Tôi không chắc chắn nếu tôi là nghĩa vụ phải làm điều này thông qua Core Graphics hoặc Core Text hoặc chính xác làm thế nào để làm cho nó hoạt động chính xác.Swift - Sân chơi - Đồ họa cốt lõi/Văn bản cốt lõi/Chế độ xem tùy chỉnh
Như bạn có thể nhìn thấy trong hình dưới đây, tôi muốn làm cho văn bản cả hai lộn ngược và bên phải lên ở hai bên xem phần tùy chỉnh của tôi
Tôi đã thử nhiều cách khác nhau để có được văn bản để làm việc nhưng tôi tiếp tục chạy bị mắc cạn. Nếu ai đó có thể chỉ cho tôi cách sửa đổi sân chơi của tôi để thêm một vài kết xuất văn bản sẽ tuyệt vời.
Playground Mã
// Playground - noun: a place where people can play
import Foundation
import UIKit
class RunwayView : UIView {
var northText : String?
var southText : String?
///Draws the "runway"
override func drawRect(rect: CGRect) {
// Setup graphics context
var ctx = UIGraphicsGetCurrentContext()
// clear context
CGContextClearRect(ctx, rect)
let parentVieBounds = self.bounds
let width = CGRectGetWidth(parentVieBounds)
let height = CGRectGetHeight(parentVieBounds)
// Setup the heights
let endZoneHeight : CGFloat = 40
/* Remember y is negative and 0,0 is upper left*/
//Create EndZones
CGContextSetRGBFillColor(ctx, 0.8, 0.8, 0.8, 1)
CGContextFillRect(ctx, CGRectMake(0, 0, width, endZoneHeight))
CGContextFillRect(ctx, CGRectMake(0, height-endZoneHeight, width, endZoneHeight))
var northString = NSMutableAttributedString(string: "36")
var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(16.0)]
var gString = NSMutableAttributedString(string:"g", attributes:attrs);
var line = CTLineCreateWithAttributedString(gString)
CGContextSetTextPosition(ctx, 10, 50);
CTLineDraw(line, ctx);
// Clean up
}
}
var outerFrame : UIView = UIView(frame: CGRectMake(20,20,400,400))
var runway1 : RunwayView = RunwayView(frame: CGRectMake(0,0,30,260))
var runway2 : RunwayView = RunwayView(frame: CGRectMake(80,0,30,340))
runway1.transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(20, 200),
CGAffineTransformMakeRotation(-0.785398163))
outerFrame.addSubview(runway1)
runway2.transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(120, 140),
CGAffineTransformMakeRotation(-0.585398163))
outerFrame.addSubview(runway2)
outerFrame.backgroundColor = UIColor.yellowColor()
outerFrame.clipsToBounds = true
// View these elements
runway1
outerFrame
runway1
Tại sao điều này lại bị lộn ngược? –
@Alexander: Trong iOS, trước tiên bạn cần lật hệ tọa độ. – zisoft
'CGRect.integral' (Swift 3) hoặc' CGRectIntegeral (rect) '(Swift 2) tốt hơn' ceil' –