Skip to content

Commit

Permalink
Add drawTextInRect (#109)
Browse files Browse the repository at this point in the history
* Add PDTextWrappingMode to Swift

* Add PDTextAlignment to Swift

* Add drawTextInRect to Swift
  • Loading branch information
gurtt authored Dec 21, 2024
1 parent 571853c commit dae77df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Sources/CPlaydate/include/CPlaydate.apinotes
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Tags:
EnumExtensibility: open
- Name: SoundWaveform
EnumExtensibility: open
- Name: PDTextWrappingMode
EnumExtensibility: open
- Name: PDTextAlignment
EnumExtensibility: open
Enumerators:
- Name: kButtonLeft
SwiftName: left
Expand Down Expand Up @@ -233,3 +237,15 @@ Enumerators:
SwiftName: flippedY
- Name: kBitmapFlippedXY
SwiftName: flippedXY
- Name: kWrapClip
SwiftName: clip
- Name: kWrapCharacter
SwiftName: character
- Name: kWrapWord
SwiftName: word
- Name: kAlignTextLeft
SwiftName: left
- Name: kAlignTextCenter
SwiftName: center
- Name: kAlignTextRight
SwiftName: right
25 changes: 25 additions & 0 deletions Sources/PlaydateKit/Core/Graphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ public enum Graphics {
public typealias StringEncoding = PDStringEncoding
public typealias PolygonFillRule = LCDPolygonFillRule
public typealias SolidColor = LCDSolidColor

public typealias TextWrap = PDTextWrappingMode
public typealias TextAlignment = PDTextAlignment

public class BitmapTable {
// MARK: Lifecycle
Expand Down Expand Up @@ -595,6 +598,28 @@ public enum Graphics {
CInt(point.y)
))
}

/// Draws the text in the given rectangle using the provided options. If no font has
/// been set with setFont, the default system font Asheville Sans 14 Light is used.
public static func drawTextInRect(
_ text: String,
in rect: Rect,
wrap: TextWrap = .clip,
aligned: TextAlignment = .left
) {
graphics.drawTextInRect.unsafelyUnwrapped(
text,
text.utf8.count,
.kUTF8Encoding,
CInt(rect.x),
CInt(rect.y),
CInt(rect.width),
CInt(rect.height),
wrap,
aligned
)
}


/// Draws an ellipse inside the rectangle `rect` of width `lineWidth` (inset from the rectangle bounds).
/// If `startAngle` != `endAngle`, this draws an arc between the given angles.
Expand Down

0 comments on commit dae77df

Please sign in to comment.