Skip to content

Commit

Permalink
Horizontal rule
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Aug 10, 2016
1 parent 349c879 commit 62e9d00
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CanvasText.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
21E1D9E21C8DF65900283961 /* AnnotationsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21E1D9E11C8DF65900283961 /* AnnotationsController.swift */; };
21E1D9E61C8DF6B800283961 /* Themeable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21E1D9E51C8DF6B800283961 /* Themeable.swift */; };
21E1D9E81C8E212300283961 /* Annotation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21E1D9E71C8E212300283961 /* Annotation.swift */; };
21E5FC691D48169900A9B281 /* HorizontalRuleAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21B1E6121CD3FF9300E2902C /* HorizontalRuleAttachment.swift */; };
21F7EEEC1CD7F77D004E80E2 /* SelectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F7EEEB1CD7F77D004E80E2 /* SelectionController.swift */; };
21F7EEEF1CD7F833004E80E2 /* SelectionControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F7EEED1CD7F82F004E80E2 /* SelectionControllerTests.swift */; };
21F7EEF11CD7F8EA004E80E2 /* NSRange+CanvasTextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F7EEF01CD7F8EA004E80E2 /* NSRange+CanvasTextTests.swift */; };
Expand Down Expand Up @@ -670,6 +671,7 @@
21DF7A4A1C8FA27D00C17A62 /* CodeBlockView.swift in Sources */,
21E1D9E21C8DF65900283961 /* AnnotationsController.swift in Sources */,
21DF7A441C8FA11100C17A62 /* CheckboxView.swift in Sources */,
21E5FC691D48169900A9B281 /* HorizontalRuleAttachment.swift in Sources */,
21AD8C301C8A430B000A53F8 /* Theme.swift in Sources */,
2138C2D11C9722DF00D93FF4 /* OrderedListItem+CanvasText.swift in Sources */,
21DF7A451C8FA11100C17A62 /* ChecklistItem+CanvasText.swift in Sources */,
Expand Down
14 changes: 10 additions & 4 deletions Sources/HorizontalRuleAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import UIKit

struct HorizontalRuleAttachment {

static let height: CGFloat = 1
static let height: CGFloat = 19

static func image(theme theme: Theme) -> UIImage? {
let width: CGFloat = 1

// Create context
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue
let context = CGBitmapContextCreate(nil, Int(height), Int(height), 8, 0, CGColorSpaceCreateDeviceRGB(), bitmapInfo)
let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, CGColorSpaceCreateDeviceRGB(), bitmapInfo)

// Background
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillRect(context, CGRect(x: 0, y: 0, width: width, height: height))

// Draw
// Line
CGContextSetFillColorWithColor(context, theme.horizontalRuleColor.CGColor)
CGContextFillRect(context, CGRect(x: 0, y: 0, width: height, height: height))
CGContextFillRect(context, CGRect(x: 0, y: ((height - 1) / 2) - 2, width: width, height: 1))

// Create image
guard let cgImage = CGBitmapContextCreateImage(context) else { return nil }
Expand Down
22 changes: 14 additions & 8 deletions Sources/TextController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,16 +481,16 @@ public final class TextController: NSObject {
let attachment: NSTextAttachment

// Horizontal rule
// if block is HorizontalRule {
// guard let image = HorizontalRuleAttachment.image(theme: theme) else { return nil }
//
// attachment = NSTextAttachment()
// attachment.image = image
// attachment.bounds = CGRect(x: 0, y: 0, width: textContainer.size.width, height: HorizontalRuleAttachment.height)
// }
if block is HorizontalRule {
guard let image = HorizontalRuleAttachment.image(theme: theme) else { return nil }

attachment = NSTextAttachment()
attachment.image = image
attachment.bounds = CGRect(x: 0, y: 0, width: textContainer.size.width, height: HorizontalRuleAttachment.height)
}

// Image
if let block = block as? CanvasNative.Image {
else if let block = block as? CanvasNative.Image {
let url = displayDelegate?.textController(self, URLForImage: block) ?? block.url

#if os(OSX)
Expand Down Expand Up @@ -770,6 +770,12 @@ extension TextController: CanvasTextStorageDelegate, NSTextStorageDelegate {
backingRange = block.range
replacement = CodeBlock.nativeRepresentation(language: language)
}

// Horizontal rule
else if string == "---" {
backingRange = block.range
replacement = HorizontalRule.nativeRepresentation() + "\n"
}
}

// Continue the previous node
Expand Down

0 comments on commit 62e9d00

Please sign in to comment.