Class: Line::Message::Builder::Flex::Text

Inherits:
Base
  • Object
show all
Includes:
Actionable, Position::Horizontal, Position::Margin, Position::Offset, Position::Vertical, Size::AdjustMode, Size::Flex, Size::Shared
Defined in:
lib/line/message/builder/flex/text.rb

Overview

Represents a text component in a LINE Flex Message.

Text components are used to display strings of text. They offer various styling options, including font size, weight, style, decoration, color, +align+ment, gravity, text +wrap+ping, line_spacing, max_lines, scaling, and more. A text component can also have an action to make it tappable.

Text components also support embedded Span components, which allow parts of the text to have different styling. Spans can be added to a text component using the span method within the Text component block.

Example: Creating a text component within a box

Line::Message::Builder.with do
flex alt_text: "Text Example" do
  bubble do
    body do
      text "Hello, Flex World!",
           size: :xl,
           color: "#FF0000",
           wrap: true do
             message "Tell me more about text", label: "More info"
      end
    end
  end
end
end

Example: Creating a text component with spans

Line::Message::Builder.with do
flex alt_text: "Span Example" do
  bubble do
    body do
      text "This message has styled spans:" do
        span "Red", color: "#FF0000"
        span " and ", size: :sm
        span "Bold", weight: :bold
      end
    end
  end
end
end

Example: Emphasising a heading and truncating a long body

Line::Message::Builder.with do
flex alt_text: "Article" do
  bubble do
    body do
      text "Ruby Taiwan Meetup", weight: :bold, size: :xl
      text long_article_body, wrap: true, max_lines: 3
    end
  end
end
end

See also:

  • https://developers.line.biz/en/reference/messaging-api/#text
  • Actionable for making the text tappable
  • Position::Horizontal for align property
  • Position::Vertical for gravity property
  • Position::Margin for margin property
  • Position::Offset for offset properties
  • Size::Flex for flex sizing property
  • Size::Shared for common size keywords (e.g., :xl, :sm)
  • Size::AdjustMode for adjust_mode property

Instance Attribute Summary collapse

Attributes inherited from Base

#context

Instance Method Summary collapse

Methods included from Size::AdjustMode

included, #shrink_to_fit!

Methods included from Size::Shared

included

Methods included from Size::Flex

included

Methods included from Position::Offset

included

Methods included from Position::Margin

included

Methods included from Position::Vertical

included

Methods included from Position::Horizontal

included

Methods included from Actionable

included, #message, #postback, #uri

Methods inherited from Base

inherited, #method_missing, #quick_reply, #respond_to_missing?

Constructor Details

#initialize(text_content = nil, context: nil, **options) ⇒ Text

Initializes a new Flex Message Text component.

[text_content] The text to display (required) [context] An optional context for the builder [options] A hash of options to set instance variables (e.g., :wrap, :color, :size, and options from included modules) [block] An optional block, typically used to define an action for the text

Raises RequiredError if text_content is nil (in to_h).



199
200
201
202
203
204
# File 'lib/line/message/builder/flex/text.rb', line 199

def initialize(text_content = nil, context: nil, **options, &)
  @text = text_content # The text content is mandatory.
  @contents = []       # Initialize contents for spans, if any.

  super(context: context, **options, &) # Sets options and evals block (for action).
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Line::Message::Builder::Base

Instance Attribute Details

#contentsObject (readonly)

Array of Span components for styled text segments.



89
90
91
# File 'lib/line/message/builder/flex/text.rb', line 89

def contents
  @contents
end

#textObject (readonly)

The actual text content to be displayed (required attribute).



86
87
88
# File 'lib/line/message/builder/flex/text.rb', line 86

def text
  @text
end

Instance Method Details

#any_content?Boolean

:nodoc:

Returns:

  • (Boolean)


239
240
241
# File 'lib/line/message/builder/flex/text.rb', line 239

def any_content?
  !contents.empty? || !text.nil?
end

#span(text_content, **options) ⇒ Object

Adds a Span component to this text component's contents.

Spans allow different styling for parts of the text, such as color, weight, size, and decoration.

[text_content] The span text content [options] Options for the span (see Span) [block] An optional block for advanced span configuration

Example

text "Message with spans:" do
span "Important", color: "#FF0000", weight: :bold
span " normal text"
end


234
235
236
# File 'lib/line/message/builder/flex/text.rb', line 234

def span(text_content, **options, &)
  @contents << Span.new(text_content, context: @context, **options, &)
end

#to_hObject

:nodoc:

Raises:



244
245
246
247
248
249
250
# File 'lib/line/message/builder/flex/text.rb', line 244

def to_h
  raise RequiredError, "text content is required for a text component" unless any_content?

  return to_sdkv2 if context.sdkv2?

  to_api
end

#wrap!Object

A convenience DSL method to set the wrap property to true.

Example

text_component.text "Long text..."
text_component.wrap! # Enables text wrapping


212
213
214
# File 'lib/line/message/builder/flex/text.rb', line 212

def wrap!
  wrap(true) # Use the setter generated by `option`
end